[英]Column count does not match in DataTable and (databound) DataGridview
我長期使用DataGridView
和DataTable
。 但是我一直無法確定為什么/如何有時(不是到處)列數在DataGridvVew
和DataTable
中不匹配,即使我將Datatable
作為DataGridView
的DataSource
也是如此。
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.