DataTable 作为 DataGrid.ItemsSource [英] DataTable as DataGrid.ItemsSource

查看:44
本文介绍了DataTable 作为 DataGrid.ItemsSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想将具有多列的 DataTable 绑定到代码隐藏中的 DataGrid

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);
}

现在如何将我的 dt 设置为 ItemsSourceDatacontext 或将其放入我的 View代码>另外,如果您可以为我提供一种直接绑定到我的 Object ff

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

推荐答案

假设您使用 WPF,只需说:

Assuming you're in WPF simply say:

DGrid.ItemsSource = dt.AsDataView();

无需在 DataGrid 上手动设置列,分配 DataTable 将为您设置这些列.

No need to manually setup your columns on your DataGrid, assigning the DataTable will set these up for you.

这篇关于DataTable 作为 DataGrid.ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆