2
Contributors
2
Replies
14
Views
4 Months
Discussion Span
Last Post by xrj
1

Let's imagine population is done so there is code, product, units, price, discount and amount.
Probably you'll need to calculate the amount by clicking the 'update amount' button:

    Private Sub DataGrid_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DataGridView1.ColumnCount = 6
        DataGridView1.Columns(0).Name = "Code"
        DataGridView1.Columns(1).Name = "Product"
        DataGridView1.Columns(2).Name = "Units"
        DataGridView1.Columns(3).Name = "Price"
        DataGridView1.Columns(4).Name = "Discount"
        DataGridView1.Columns(5).Name = "Amount"

        Dim row As String()
        row = New String() {"3", "Product A", "100", "5.5", "0.0", "0"}
        DataGridView1.Rows.Add(row)
        row = New String() {"5", "Product B", "20", "15.0", "3.0", "0"}
        DataGridView1.Rows.Add(row)
        row = New String() {"6", "Product C", "300", "2.5", "5.0", "0"}
        DataGridView1.Rows.Add(row)
        row = New String() {"8", "Product D", "70", "10", "0.0", "0"}
        DataGridView1.Rows.Add(row)
        DataGridView1.Columns(0).Visible = False

        Dim btn As New DataGridViewButtonColumn()
        DataGridView1.Columns.Add(btn)
        btn.HeaderText = "Update"
        btn.Text = "Update Amount"
        btn.Name = "btnUpdate"
        btn.UseColumnTextForButtonValue = True

    End Sub
    Function parseNum(value As String) As Double
        Static us As New Globalization.CultureInfo("en-US")
        Dim dbl As Double
        Double.TryParse(value, _
            Globalization.NumberStyles.Any, us, dbl)
        Return dbl
    End Function
    Dim us As New Globalization.CultureInfo("en-US")
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Try
            With DataGridView1
                If .Columns(e.ColumnIndex).Name = "btnUpdate" Then ' update button pressed
                    Dim units, price, discount, amount As Double
                    Dim colAmount As Int32
                    For i = 0 To .Columns.Count - 1
                        Dim value = .Rows(e.RowIndex).Cells(i).Value
                        Select Case LCase(.Columns(i).Name)
                            Case "units" : units = parseNum(value)
                            Case "price" : price = parseNum(value)
                            Case "discount" : discount = parseNum(value)
                            Case "amount" : colAmount = i
                        End Select
                    Next
                    amount = units * price * (1 - discount / 100.0)
                    .Rows(e.RowIndex).Cells(colAmount).Value = amount.ToString(us)
                End If
            End With
        Catch ex As Exception

        End Try
    End Sub
Votes + Comments
Nice!
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.