Muruli madhav

Muruli madhav

  • NA
  • 205
  • 3.7k

C# WPF How to bind a nested class to nested datagrid

Apr 30 2020 12:36 AM
in WPF, How to bind data from nested classs to a nested datagrid? Why nested datagrid unable to read the class? what error i am doing?
  1. public class Student    
  2. {    
  3.       public int Id { getset; }    
  4.       public string Name { getset; }    
  5.       public DateTime Birthday { getset; }    
  6.       public Marks myMarks { getset; }    
  7. }    
  8.     
  9. public class Marks    
  10. {    
  11.       public double English { getset; }    
  12.       public double Maths { getset; }    
  13.       public double Science { getset; }    
  14. }  
itemsource for main-datagrid
  1. private void Page_Loaded(object sender, RoutedEventArgs e)    
  2. {    
  3.    List<Student> myStudents = new List<Student>();    
  4.     
  5.     Marks JohnMark = new Marks();    
  6.     JohnMark.English = 75;    
  7.     JohnMark.Maths = 85;    
  8.     JohnMark.Science = 95;    
  9.     
  10.     Marks RichardMark = new Marks();    
  11.     RichardMark.English = 70;    
  12.     RichardMark.Maths = 80;    
  13.     RichardMark.Science = 90;    
  14.     
  15.     Marks SamMark = new Marks();    
  16.     SamMark.English = 72;    
  17.     SamMark.Maths = 82;    
  18.     SamMark.Science = 92;    
  19.     
  20.     myStudents.Add(new Student() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23), myMarks = JohnMark });    
  21.     myStudents.Add(new Student() { Id = 2, Name = "Richard Doe", Birthday = new DateTime(1974, 1, 17), myMarks = RichardMark });    
  22.     myStudents.Add(new Student() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2), myMarks = SamMark });    

  23.     DG_myStudents.ItemsSource = myStudents;    
  24. }
and below is my XAML code, Am i doing any error in class structure? Please help me.
  1. <DataGrid x:Name="DG_myStudents" RowDetailsVisibilityMode="Visible">    
  2.         <DataGrid.RowDetailsTemplate>    
  3.             <DataTemplate>    
  4.                 <DataGrid x:Name="DG_myMarks" ItemsSource="{Binding myMarks}" />    
  5.             </DataTemplate>    
  6.         </DataGrid.RowDetailsTemplate>    
  7. </DataGrid>  
Nothing, the nested datagrid "DG_myMarks" is not showing any data. And when i bind "myMarks" to a label to just to see any thing happening there, then label is showing "Marks class name" like this "MyNameSpace.myPage+Marks"

Answers (3)