所以,我创建一个自定义控件的实例,设置DataSource属性到DataTable的一个实例,以及...网格中没有列。我的DataTable具有列(和行),AutoGenerateColumns为true,但没有列出现在网格列集合中。事实上,它的自定义控件在这一点上变得无关紧要,因为DataGridView控件执行相同。
如果我将网格添加到表单的Controls集合中,数据绑定工作,网格有我的列。
为什么这样?
public Form1() { InitializeComponent(); DataTable dataTable = new DataTable(){TableName =Bob}; dataTable.Columns.Add(One,typeof(String)); dataTable.Columns.Add(Two,typeof(String)); dataTable.Columns.Add(Three,typeof(String)); dataTable.Rows.Add(a,b,c); dataTable.Rows.Add(d,e,f); dataTable.Rows.Add(g,h,i); DataGridView grid = new DataGridView(); grid.DataSource = dataTable; int n1 = grid.Columns.Count; //返回零 this.Controls.Add(grid); //为什么要这样做? grid.DataSource = null; grid.DataSource = dataTable; int n2 = grid.Columns.Count; //返回三个}谢谢, Ross
解决方案David Hall的评论引导我进入这里,这导致我研究了网格的BindingContext。果然,它是空的,但是通过为网格控件创建一个新的BindingContext,将网格绑定到DataTable现在填充列集合。
code> public Form1() { InitializeComponent(); DataTable dataTable = new DataTable(){TableName =Bob}; : DataGridView grid = new DataGridView {Name =Tom}; grid.BindingContext = new BindingContext(); grid.DataSource = dataTable; int n1 = grid.Columns.Count; //返回三个}奇怪的是,您设置绑定上下文的顺序或数据源似乎并不重要!
I have a custom control which inherits from DataGridView. It augments the control with some additional functionality (which exports the contents of each cell). I would like to use this functionality, but don't need the UI of the grid itself.
So, I create an instance of the custom control, set the DataSource property to an instance of a DataTable, and... no columns in the grid. My DataTable has columns (and rows), AutoGenerateColumns is true, but no columns appear in the grid "Columns" collection. The fact that its a custom control becomes irrelevant at this point, because the DataGridView control does the same.
If I add the grid to a form's "Controls" collection, the data binding works, and the grid has my columns.
Why is that ?
public Form1() { InitializeComponent(); DataTable dataTable = new DataTable() { TableName = "Bob" }; dataTable.Columns.Add("One",typeof(String)); dataTable.Columns.Add("Two", typeof(String)); dataTable.Columns.Add("Three", typeof(String)); dataTable.Rows.Add("a", "b", "c"); dataTable.Rows.Add("d", "e", "f"); dataTable.Rows.Add("g", "h", "i"); DataGridView grid = new DataGridView(); grid.DataSource = dataTable; int n1 = grid.Columns.Count; // returns zero this.Controls.Add(grid); // why do I have to do this ? grid.DataSource = null; grid.DataSource = dataTable; int n2 = grid.Columns.Count; // returns three }Thanks, Ross
解决方案David Hall's comment led me to here, which led me to look into the BindingContext of the grid. Sure enough, it's null, but by creating a new BindingContext for the grid control, binding the grid to the DataTable now populates the "Columns" collection.
public Form1() { InitializeComponent(); DataTable dataTable = new DataTable() { TableName = "Bob" }; : DataGridView grid = new DataGridView { Name = "Tom" }; grid.BindingContext = new BindingContext(); grid.DataSource = dataTable; int n1 = grid.Columns.Count; // returns three }Curiously enough, the order in which you set the binding context or the data source appears not to matter !
更多推荐
数据绑定不在Form.Controls集合中的DataGridView控件?
发布评论