2008-11-06 85 views
10

我遇到以下一段代碼有問題。當我去通過與調試我得到一個異常,當涉及到以下行:無法添加列,因爲它的CellType屬性爲空例外

dgvCalls.Columns.Insert(1, msisnnColumn); 

我得到一個異常:不能添加

列,因爲它 CellType屬性爲null。

奇怪的是,我創建了一些其他DataGridViews相同的過程,它工作正常。

if (!(dgvCalls.Columns.Contains("DirectionImage"))) 
       { 
        directionIconColumn = new DataGridViewImageColumn(); 
        directionIconColumn.Name = "DirectionImage"; 
        directionIconColumn.HeaderText = ""; 
        dgvCalls.Columns.Insert(0, directionIconColumn); 
        directionIconColumn.CellTemplate = new DataGridViewImageCell(); 
       } 
       if (!(dgvCalls.Columns.Contains("msisndColumn"))) 
       { 
        msisnnColumn = new DataGridViewColumn(); 
        msisnnColumn.Name = "msisndColumn"; 
        msisnnColumn.HeaderText = "Klic"; 
        dgvCalls.Columns.Insert(1, msisnnColumn); 
        msisnnColumn.CellTemplate = new DataGridViewTextBoxCell(); 
       } 

有什麼建議嗎?

+0

您可以通過突出顯示代碼並單擊編輯textarea上方的小0101010圖標,以易於閱讀的格式顯示代碼。 – DOK 2008-11-06 16:40:11

回答

17
dgvCalls.Columns.Insert(1, msisnnColumn); 
msisnnColumn.CellTemplate = new DataGridViewTextBoxCell(); 

嘗試翻轉這兩行。這可能會訣竅。

+0

謝謝!有用。我想知道爲什麼它在訂單不同的另一種形式中起作用! – 2008-11-06 16:47:19

相關問題