Search code examples
vb.netdatagridviewdoublevalue-type

Datagridview valuetype property not changing


I have some code to convert the datatypes of some of the columns of a datagridview into a double, as they should be displaying as currency in my grid view. However when I run the code, the values are still displayed as a normal decimal. Am I using the wrong code to do what I want, or can this simply not be done? I appreciate any help you can give me. Here is my code:

For i = 0 To ds.Tables(0).Columns.Count -1
    InventoryDataGridView.ColumnCount = ds.Tables(0).Columns.Count
    InventoryDataGridView.Columns(1).ValueType = GetType(Date) 'this type changes
    InventoryDataGridView.Columns(2).ValueType = GetType(Double) 'this line and the line below do not occur in the grid view
    InventoryDataGridView.Columns(4).ValueType = GetType(Double)
    InventoryDataGridView.Columns(i).Name = InventoryColumns(i)
Next

Solution

  • Use the CellFormatting event to show your currency:

    Private Sub InventoryDataGridView_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles InventoryDataGridView.CellFormatting
      If (e.ColumnIndex = 2) Then
        e.Value = String.Format("{0:$0.00}", e.Value)
        e.FormattingApplied = True
      End If
    End Sub