How can I detect the cell value changed of a specific datagridview column? - VB.NET

Muhammad Saqib picture Muhammad Saqib · Jul 29, 2014 · Viewed 25.1k times · Source

I want to detect the cell value has been changed of a specific column.

My Datagridview name is DGV_Products and it has 6 columns.

Product ID | Descriptions | Quantity | Unit Price | Discount | Amount
G 01       |  Gallon #01  |    2     |   1850     |    100   |  3600 
G 02       |  Gallon #02  |    1     |   1850     |    50    |  1800

I want to fire some code only when the Quantity column cell value changed not when the discount column value change. How can I fulfill my task? Currently the codes, what I have tried which is;

 Private Sub DGV_Products_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Products.CellValueChanged

    If DGV_Products.Rows.Count > 0 Then

        Dim Quantity As Integer = CInt(DGV_Products.CurrentRow.Cells(2).Value)
        Dim UnitProce As Integer = CInt(DGV_Products.CurrentRow.Cells(3).Value)
        Dim DiscountPrice As Integer = CInt(DGV_Products.CurrentRow.Cells(4).Value)

        Dim TotalDiscount As Integer = DiscountPrice * Quantity
        Dim Amount As Integer = UnitProce * Quantity
        Amount = Amount - TotalDiscount

        DGV_Products.CurrentRow.Cells(4).Value = TotalDiscount
        DGV_Products.CurrentRow.Cells(5).Value = Amount


        RefreshTotal()

    End If
End Sub

*Apologies for bad english.

Answer

Muhammad Saqib picture Muhammad Saqib · Jul 29, 2014

Oh, I got the the solution. I use e.ColumnIndex property and my problem has solved.

 If DGV_Products.Rows.Count > 0 Then

        If e.ColumnIndex = 2 Then
            Dim Quantity As Integer = CInt(DGV_Products.Rows(e.RowIndex).Cells(2).Value)
            Dim UnitPrice As Integer = CInt(DGV_Products.Rows(e.RowIndex).Cells(3).Value)
            Dim UnitDisocunt As Integer = GetDiscountByProductID(DGV_Products.Rows(e.RowIndex).Cells(0).Value)
            Dim TotalDiscount As Integer = UnitDisocunt * Quantity
            DGV_Products.Rows(e.RowIndex).Cells(4).Value = TotalDiscount
            Dim Ammount As Integer = UnitPrice * Quantity
            Ammount = Ammount - TotalDiscount
            DGV_Products.Rows(e.RowIndex).Cells(5).Value = Ammount
            RefreshTotal()
        End If

    End If

Thank you for your time.