如何在 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 进行了搜索,但奇怪的是我找不到解决方案。
尝试绑定到数据集中的表之一:
datagrid.ItemsSource = dataset.Tables[0].DefaultView;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)