Click here to Skip to main content
14,665,492 members
Rate this:
Please Sign up or sign in to vote.
See more:
How to bind data from nested classs to a nested datagrid? Why nested datagrid unable to read the class? what error i am doing?
public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
        public Marks myMarks { get; set; }
    }

    public class Marks
    {
        public double English { get; set; }
        public double Maths { get; set; }
        public double Science { get; set; }
    }
Itemsource for main-datagrid
private void Page_Loaded(object sender, RoutedEventArgs e)
{
   List<Student> myStudents = new List<Student>();

    Marks JohnMark = new Marks();
    JohnMark.English = 75;
    JohnMark.Maths = 85;
    JohnMark.Science = 95;

    Marks RichardMark = new Marks();
    RichardMark.English = 70;
    RichardMark.Maths = 80;
    RichardMark.Science = 90;

    Marks SamMark = new Marks();
    SamMark.English = 72;
    SamMark.Maths = 82;
    SamMark.Science = 92;

    myStudents.Add(new Student() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23), myMarks = JohnMark });
    myStudents.Add(new Student() { Id = 2, Name = "Richard Doe", Birthday = new DateTime(1974, 1, 17), myMarks = RichardMark });
    myStudents.Add(new Student() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2), myMarks = SamMark });

    DG_myStudents.ItemsSource = myStudents;
}


What I have tried:

and below is my XAML code, Am i doing any error in class structure? Please help me.
<DataGrid x:Name="DG_myStudents" RowDetailsVisibilityMode="Visible">
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid x:Name="DG_myMarks" ItemsSource="{Binding myMarks}" />
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>

The nested datagrid "DG_myMarks" is not showing any data. And when i bind "myMarks" to a label tfor just to check any thing happening there, then label is showing "Marks class name" like this "MyNameSpace.myPage+Marks"
Posted
Updated 29-Apr-20 23:16pm

Rate this:
Please Sign up or sign in to vote.

Solution 1

Can you check this
How to: Add Row Details to a DataGrid Control - WPF | Microsoft Docs[^]

https://www.wpf-tutorial.com/datagrid-control/details-row
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi, its solved in this way by editing "class Student" by modifing "myMarks" to collection (e.g. list).
public class Student
 {
   public int Id { get; set; }
   public string Name { get; set; }
   public DateTime Birthday { get; set; }
   public List<Marks> myMarks { get; set; } = new List<Marks>();
 }
and then little modification in the list-myStudents
private void Page_Loaded(object sender, RoutedEventArgs e)
 {
   List<Student> myStudents = new List<Student>();

   Marks JohnMark = new Marks();
   JohnMark.English = 75;
   JohnMark.Maths = 85;
   JohnMark.Science = 95;

   Marks RichardMark = new Marks();
   RichardMark.English = 70;
   RichardMark.Maths = 80;
   RichardMark.Science = 90;

   Marks SamMark = new Marks();
   SamMark.English = 72;
   SamMark.Maths = 82;
   SamMark.Science = 92;

   Student stud = new Student() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23) };
   stud.myMarks.Add(JohnMark);
   myStudents.Add(stud);
   stud = new Student() { Id = 2, Name = "Richard Doe", Birthday = new DateTime(1974, 1, 17) };
   stud.myMarks.Add(RichardMark);
   myStudents.Add(stud);
   stud = new Student() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2) };
   stud.myMarks.Add(SamMark);
   myStudents.Add(stud);
   DG_myStudents.ItemsSource = myStudents;
 }

Thanks to Peter Fleischer from Microsoft Q&A forum.
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100