我试图在编辑值时使 DataGridView 列在 vb.net 中不以货币格式显示。
我还在这篇文章中附上了一个 GIF 文件。
或者我使用的代码可能是错误的
或者我需要采取其他方法吗?
请指导我
谢谢
以下表格1中的代码
Public Class Form1
Private BindingSource1 As BindingSource = Nothing
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' To reduce the flickering...
DataGridView1.GetType().
GetProperty("DoubleBuffered",
BindingFlags.Instance Or BindingFlags.NonPublic).
SetValue(DataGridView1, True)
End Sub
Private Sub CalculateTotal()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim quantity As Double = 0
Dim price As Double = 0
If Not IsDBNull(item.Cells("Qty").Value) Then
Double.TryParse(CType(item.Cells("Qty").Value, String), quantity)
End If
If Not IsDBNull(item.Cells("PRICE").Value) Then
Double.TryParse(CType(item.Cells("PRICE").Value, String), price)
End If
tot += (quantity * price)
Next item
lblTotal.Text = tot.ToString("N")
End Sub
Protected Sub updateDataSource()
BindingSource1 = New BindingSource With {.DataSource = New BindingList(Of Products)}
DataGridView1.DataSource = BindingSource1
DataGridView1.AutoGenerateColumns = False
DataGridView1.AllowUserToAddRows = False
DataGridView1.Columns("PRICE").DefaultCellStyle.Format = "c2"
DataGridView1.Columns("PRICE").DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US")
DataGridView1.Columns("TOTAL").DefaultCellStyle.Format = "c2"
DataGridView1.Columns("TOTAL").DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US")
BindingSource1.AddNew()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
updateDataSource()
End Sub
Private Sub DataGridView1_CellEndEdit(
sender As Object,
e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Dim dgv = DirectCast(sender, DataGridView)
Dim PRICE As Double = Nothing, QTY As Double = Nothing, TOTAL As Double = Nothing
If DataGridView1.Rows(e.RowIndex).Cells("QTY").Value IsNot Nothing Then
If Not Double.TryParse(DataGridView1.Rows(e.RowIndex).Cells("QTY").Value.ToString(), QTY) Then
QTY = 0
End If
Else
QTY = 0
End If
DataGridView1.Rows(e.RowIndex).Cells("QTY").Value = QTY
If DataGridView1.Rows(e.RowIndex).Cells("PRICE").Value IsNot Nothing Then
If Not Double.TryParse(DataGridView1.Rows(e.RowIndex).Cells("PRICE").Value.ToString(), PRICE) Then
PRICE = 0
End If
Else
PRICE = 0
End If
DataGridView1.Rows(e.RowIndex).Cells("PRICE").Value = PRICE
TOTAL = (QTY * PRICE)
DataGridView1.Rows(e.RowIndex).Cells("TOTAL").Value = TOTAL
CalculateTotal()
End Sub
End Class
Public Class Products
Public Property PRODUCTNAME() As String
Public Property QTY() As Double
Public Property PRICE() As Double
Public Property TOTAL() As Double
End Class
上述代码在gif文件中的结果
按照这个链接!
解决办法是在datagridview CellBeginEdit事件中添加一个DefaultCellStyle.Format
Protected Sub updateDataSource()
BindingSource1 = New BindingSource With {.DataSource = New BindingList(Of Products)}
DataGridView1.DataSource = BindingSource1
DataGridView1.AutoGenerateColumns = False
DataGridView1.AllowUserToAddRows = False
DataGridView1.Columns("PRICE").DefaultCellStyle.Format = "c2"
DataGridView1.Columns("PRICE").DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US")
DataGridView1.Columns("TOTAL").DefaultCellStyle.Format = "c2"
DataGridView1.Columns("TOTAL").DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US")
BindingSource1.AddNew()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
updateDataSource()
End Sub
Private Sub DataGridView1_CellEndEdit(
sender As Object,
e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
If e.RowIndex >= 0 AndAlso
dgv.Columns(e.ColumnIndex) Is DataGridView1.Columns("PRICE") Then
Dim price1 As Double = Convert.ToDouble(dgv.Rows(e.RowIndex).Cells("PRICE").Value)
DataGridView1.Columns("PRICE").DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US")
DataGridView1.Columns("PRICE").DefaultCellStyle.Format = String.Format("c2")
End Sub
Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
Dim dgv = DirectCast(sender, DataGridView)
If e.RowIndex >= 0 AndAlso
dgv.Columns(e.ColumnIndex) Is DataGridView1.Columns("PRICE") Then
dgv.Columns("PRICE").DefaultCellStyle.Format = ""
End If
End Sub