2008-11-06 196 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

相关问题