I’m trying to make the DataGridView columns when editing the value appear not in currency format in vb.net.
I also attach a GIF file in this post.
Or maybe the code I’m using is wrong
or is there another way I need to do?
Please Guide me
Thanks
Below Code in form1
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
The result of the above code in a gif file
5
as per this link!
The solution is to add a DefaultCellStyle.Format in event datagridvidew CellBeginEdit
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
2