Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable as DataGrid.ItemsSource

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

like image 536
WiiMaxx Avatar asked Feb 18 '13 17:02

WiiMaxx


1 Answers

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.

like image 167
Lee Harrison Avatar answered Sep 29 '22 14:09

Lee Harrison