hi i want to bind a DataTable
with multiple columns to an DataGrid
in codebehind
var dt = new DataTable();
dt.Columns.Add(new DataColumn("1"));
dt.Columns.Add(new DataColumn("2"));
dt.Columns.Add(new DataColumn("3"));
dt.Rows.Add(ff.Mo);
dt.Rows.Add(ff.Di);
dt.Rows.Add(ff.Mi);
dt.Rows.Add(ff.Do);
dt.Rows.Add(ff.Fr);
dt.Rows.Add(ff.Sa);
dt.Rows.Add(ff.So);
// ff is a object that contains List<myCellObj>
DataGrid DGrid = new DataGrid();
for (int i = 0; i < 3; i++)
{
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.HeaderTemplate = HeaderDt;
templateColumn.CellTemplate = ItemDt; //specified DataTemplate for myCellObj
DGrid.Columns.Add(templateColumn);
}
now how do i set my dt
as ItemsSource
, Datacontext
or what ever to get it in to my View
also if you could provide me a way to bind directly to my Object ff
anything that could help is greatly appreciated
Assuming you're in WPF simply say:
DGrid.ItemsSource = dt.AsDataView();
No need to manually setup your columns on your DataGrid, assigning the DataTable will set these up for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With