How To Compare values in a TextBox

KwebenaAcquah-9104 306 Reputation points
2021-01-18T01:30:03.28+00:00

i have a datagrid and a textbox present in my project, the Datagrid loads all data from SQL database and when i select each row it all shows in the texbox1 and my question is. can i be able to compare these values in the textbox and grade each by highest value and save the graded values into the datagrid and to the database ? please can some one give me a lucid and comprehensive answer or at least try to provide solution to my problem.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,408 questions
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-01-19T06:58:26.5+00:00

    Sample data in the database:
    58887-capture.png
    Sample code:

            private void button_Click(object sender, RoutedEventArgs e)  
            {  
                DataTable dataTable = GetData();  
                dataGrid.ItemsSource = dataTable.DefaultView;  
                mathListView.ItemsSource = GetRowCollection(dataTable, "Math");  
                scienceListView.ItemsSource = GetRowCollection(dataTable, "Science");  
                englishListView.ItemsSource = GetRowCollection(dataTable, "English");  
            }  
            private EnumerableRowCollection<Tuple<object, object>> GetRowCollection(DataTable dataTable, string subjectName)  
            {  
                var scoreList = from row in dataTable.AsEnumerable()  
                                where row["SubjectName"].ToString() == subjectName  
                                orderby row["Score"] descending  
                                select new Tuple<object, object>(  
                                    row["Name"],  
                                    row["Score"]  
                                );  
                return scoreList;  
            }  
            public DataTable GetData()  
            {  
                string connString = @"connString";  
                using (SqlConnection connection = new SqlConnection(connString))  
                {  
                    connection.Open();  
                    string sql = @"select stu.Name , sub.SubjectName, ss.Score" +  
                                    " from Student stu" +  
                                    " join Student_Subject ss" +  
                                    " on stu.ID = ss.StudentID" +  
                                    " join Subject sub " +  
                                    " on sub.SubjectID = ss.SubjectID";  
                    using (SqlCommand command = new SqlCommand(sql, connection))  
                    {  
                        DataTable dataTable = new DataTable();  
                        dataTable.Load(command.ExecuteReader());  
      
                        return dataTable;  
                    }  
                }  
            }  
    

    I used ListView instead of Textbox because I thought it would be more beautiful.

        <ListView x:Name="mathListView" HorizontalAlignment="Left" Height="196" Margin="334,170,0,0" VerticalAlignment="Top" width="126">  
                <ListView.View>  
                    <GridView>  
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Item1}" />  
                        <GridViewColumn Header="Score" DisplayMemberBinding="{Binding Item2}"/>  
                    </GridView>  
                </ListView.View>  
            </ListView>  
    

    Result:
    58952-11.png
    Does this meet your expectations?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful