如何设置datagrid列的单元格类型?

时间:2019-07-02 10:30:14

标签: windows vb.net datagrid

我正在向数据网格添加新列以存储一行总计qty * Cost
当我尝试添加列时,出现异常提示

  

System.InvalidOperationException:'无法添加列,因为其CellType属性为null。'

我尝试设置单元格类型,但我无法正确设置类型

Dim dt As DataTable = Me.DsOppQuoteDetail.tblOppQuoteDetail
Dim dr As DataRow

Dim dc As New DataGridViewColumn
With dc
    .HeaderText = "Item Total"
    .Name = "UnitTotal"
     .CellType = DataGridTextBox

End With

DGV_OppQuoteDetail.Columns.Insert(6, dc)

将CellType设置为DataGridTextBox会产生错误

如果我将列更改为:

Dim dc As New DataGridTextBoxColumn
With dc
    .HeaderText = "Item Total"
End With

DGV_OppQuoteDetail.Columns.Insert(6, dc)

然后我无法插入它,因为它是DataGrid.Insert命令的错误类型

1 个答案:

答案 0 :(得分:0)

Dim dc As New DataGridViewTextBoxColumn
dc.HeaderText = "SomeText"
dc.Name = "colWhateverName"
DGV_OppQuoteDetail.Columns.Add(dc)

尝试一下,让我知道。仅在措辞上略有不同。另外,从您的代码段来看,这似乎还不错,但请确保在添加列之前不要添加任何行。