locked
C# WPF How to bind a nested class to nested datagrid in “Datagrid Row-Details”? RRS feed

  • Question

  • 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 "DG_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;
    
        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;
    }

    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"


    Thursday, April 30, 2020 2:19 AM

Answers

  • Hi Mmkottari,
    Based on your description, it is mostly related to WPF. So it is recommended to ask the questions in Microsoft Q&A forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mmkottari Thursday, April 30, 2020 8:31 AM
    Thursday, April 30, 2020 3:18 AM

All replies

  • Hi Mmkottari,
    Based on your description, it is mostly related to WPF. So it is recommended to ask the questions in Microsoft Q&A forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mmkottari Thursday, April 30, 2020 8:31 AM
    Thursday, April 30, 2020 3:18 AM
  • Hi, thanks for guidence. I posted the question on Microsoft Q&A forum. Please remove my question from this form if possible. Because i guess i dont have option to remove it from myside.
    And this question is solved immediately in Microsoft Q&A forum. Thanks a lot
    • Edited by Mmkottari Thursday, April 30, 2020 8:31 AM
    Thursday, April 30, 2020 4:33 AM