Datagridview valuetype属性不变

时间:2015-03-12 21:26:32

标签: vb.net datagridview double value-type

我有一些代码可以将datagridview的某些列的数据类型转换为double,因为它们应该在我的网格视图中显示为货币。 但是,当我运行代码时,值仍显示为正常小数。我使用错误的代码来做我想做的事情,还是可以做到这一点?我感谢你能给我的任何帮助。 这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

使用CellFormatting事件来显示您的货币:

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