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

3

6

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.

Praedial answered 29/7, 2014 at 16:30 Comment(0)
P
9

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.

Praedial answered 29/7, 2014 at 16:36 Comment(0)
B
0

The following worked for me

if (Visible && !(e.ColumnIndex == 0))
{
    phoneEdited = true;
    MessageBox.Show("A Phone entry has been entered");
}
Biotic answered 26/2, 2017 at 8:9 Comment(0)
I
0
Private Sub dataGrid_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dataGrid.CellBeginEdit
    If e.ColumnIndex = 0 Then
        MsgBox("Detected First Column")
    End If
End Sub
Interception answered 18/8, 2017 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.