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

  • Thread starter Thread starter Mmkottari
  • Start date Start date
M

Mmkottari

Guest
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"

Continue reading...
 
Back
Top