数据绑定不在Form.Controls集合中的DataGridView控件?

编程入门 行业动态 更新时间:2024-10-21 07:30:47
本文介绍了数据绑定不在Form.Controls集合中的DataGridView控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个继承自DataGridView的自定义控件。它增加了一些附加功能(导出每个单元格的内容)的控件。我想使用此功能,但不需要网格本身的UI。

所以,我创建一个自定义控件的实例,设置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控件?

本文发布于:2023-11-06 13:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   控件   数据   Form   DataGridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!