How to make the DatagridView columns when editing the value appear not in currency format in vb.net

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật