Sample data in the database:
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:
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.