如何绑定一个DataGrid到DataTable所有后面的代码? [英] How to Bind a DataGrid to a DataTable all in code behind?
问题描述
如何绑定一个DataGrid在后面的C#代码的DataTable? (所有的控件都在运行时产生的,所以没有请XAML)
How to bind a DataGrid to a DataTable in C# code behind? (All controls are generated in run-time, so no XAML please)
我试图绑定(),设置的DataContext,设置的ItemsSource,但都不起作用
I tried Binding(), set the DataContext, set the ItemsSource, but all doesn't work:
/* 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上自动显示。
All I need to do is to Bind a DataGrid to a DataTable, so when there is new rows added to the DataTable, it will show automatically on the DataGrid.
我搜索全部通过计算器和谷歌,但奇怪的是,我不能找到一个解决方案。
I search all through stackoverflow and google but strangely I cannot find a solution.
推荐答案
尝试绑定到你的DataSet中的表之一:
Try binding to one of the Tables in your dataset:
datagrid.ItemsSource = dataset.Tables[0].DefaultView;
这篇关于如何绑定一个DataGrid到DataTable所有后面的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!