Search code examples
windowsvb.netdatagrid

How do i set the celltype for a datagrid column?


I am adding a new column to a datagrid to store a row total of qty*Cost
When I try to add the column I get an exception saying

System.InvalidOperationException: 'Column cannot be added because its CellType property is null.'

I've tried to set the cell type but I can't get the type right

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)

Setting the CellType to DataGridTextBox produces an error

If I change the the column to:

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

DGV_OppQuoteDetail.Columns.Insert(6, dc)

then I can't insert it because it's the wrong type for the DataGrid.Insert command


Solution

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

    Try this and let me know. Only slightly different in terms of wording. Also from your code snippet it seems fine but make sure you don't add any rows before adding a column.