Dear sir,
I'm trying to make the DataGridView columns when editing the value appear not in number format.in vb.net
I also attach a GIF file in this post.
Please guide me
Thanks
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
Result code in gif