[英]DataTable as DataGrid.ItemsSource
hi i want to bind a DataTable
with multiple columns to an DataGrid
in codebehind嗨,我想将具有多列的
DataTable
绑定到代码隐藏中的DataGrid
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
现在我如何将我的
dt
设置为ItemsSource
、 Datacontext
或任何将它放入我的View
如果你能提供我一种直接绑定到我的Object ff
anything that could help is greatly appreciated任何可以提供帮助的东西都非常感谢
Assuming you're in WPF simply say:假设您在 WPF 中,只需说:
DGrid.ItemsSource = dt.AsDataView();
No need to manually setup your columns on your DataGrid, assigning the DataTable will set these up for you.无需在 DataGrid 上手动设置列,分配 DataTable 将为您设置这些列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.