c# - How to bind innergrid in datagrid? -
basically new databinding here tried insert inner grid in each row displaying marks. can 1 me how achieve one.
xaml code:
<grid> <datagrid x:name="datagrid" autogeneratecolumns="true" rowdetailsvisibilitymode="visiblewhenselected"> <datagrid.rowdetailstemplate> <datatemplate> <datagrid itemssource="{binding marks}" autogeneratecolumns="true"/> </datatemplate> </datagrid.rowdetailstemplate> </datagrid> </grid>
below code contains 2 classes 1 students , marks
code:
public partial class mainwindow : window { public mainwindow() { initializecomponent(); datagrid.itemssource = getstudentsinfo(); } private list<student> getstudentsinfo() { list<student> stdcoll = new list<student>(); stdcoll.add(new student() { id = 1, name = "aaa", marks = new marks() { sub1 = 75, sub2 = 80, sub3 = 90 } }); stdcoll.add(new student() { id = 2, name = "bbb", marks = new marks() { sub1 = 45, sub2 = 50, sub3 = 70 } }); stdcoll.add(new student() { id = 3, name = "ccc", marks = new marks() { sub1 = 35, sub2 = 80, sub3 = 60 } }); stdcoll.add(new student() { id = 4, name = "ddd", marks = new marks() { sub1 = 75, sub2 = 40, sub3 = 90 } }); return stdcoll; } } public class student { public int id { get; set; } public string name { get; set; } public marks marks { get; set; } } public class marks { public int sub1 { get; set; } public int sub2 { get; set; } public int sub3 { get; set; } }
your inner datagrid has bind list outer one.
make changes mark class
public class mark { public int sub { get; set; } }
change mark
property list
public marks marks { get; set; }
to
public list<mark> marks { get; set; }
when add student, give them new list
instead of mark
stdcoll.add(new student() { id = 4, name = "ddd", marks = new list<mark>() { new mark(){ sub = 90; } } });
then minor name change binding
itemssource="{binding marks}"
so binding looks property marks
inside class student
as recommendation, mvvm pattern , how use it.
Comments
Post a Comment