【发布时间】:2011-08-14 11:22:59
【问题描述】:
如何在后面的 C# 代码中将 DataGrid 绑定到 DataTable? (所有控件都是在运行时生成的,所以请不要使用 XAML)
我试过Binding(),设置DataContext,设置ItemsSource,但都不起作用:
/* Binding() fails */
Binding bind = new Binding();
bind.Source = dataset;
bind.Path = new PropertyPath("dataset.Tables");
datagrid.SetBinding(DataSet, bind);
/* DataContext fails */
datagrid.DataContext = dataset.Tables;
/* ItemsSource fails */
datagrid.ItemsSource = dataset.Tables;
我需要做的就是将一个DataGrid绑定到一个DataTable,这样当DataTable中添加新行时,它会自动显示在DataGrid上。
我通过 stackoverflow 和 google 搜索了所有内容,但奇怪的是我找不到解决方案。
【问题讨论】:
-
您的意思是将数据表绑定到数据网格,而不是将数据网格绑定到数据表。
-
订阅DataTable的TableNewRow事件并赋值