繁体   English   中英

DataTable 和(数据绑定)DataGridview 中的列数不匹配

[英]Column count does not match in DataTable and (databound) DataGridview

我长期使用DataGridViewDataTable 但是我一直无法确定为什么/如何有时(不是到处)列数在DataGridvVewDataTable中不匹配,即使我将Datatable作为DataGridViewDataSource也是如此。

myGrid.DataSource = myTable;
myGrid.Refresh();
MessageBox.Show(myTable.Columns.Count+"--"+myGrid.Columns.Count);// shows 5--0

编辑:(回答第一条评论)这里我的数据表被创建为

DataTable myTable = new DataTable();
myTable.Columns.Add("Col1", typeof(int));
myTable.Columns.Add("Col2", typeof(string));
myTable.Columns.Add("Col3", typeof(string));

当我将 DataGridView 控件放在 TabControl 中时,我遇到了同样的问题。

DataGridView 控件位于 TabControl 的第二个选项卡上,默认情况下它在启动时不显示,当我切换到第二个选项卡时,我在实现的 DataGridView_CellFormatting 事件中遇到了问题。

我从微软找到了这个

“在显示标签页之前不会创建 TabPage 中包含的控件,并且在显示标签页之前不会激活这些控件中的任何数据绑定。”

我通过将以下代码行添加到我的 Form_Load 事件来解决我的问题:

tabControl1.SelectTab(1); //Selects the second tab

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM