如何检测特定datagridview列的单元格值更改? - VB.NET

时间:2014-07-29 16:30:35

标签: vb.net datagridview datagridviewcolumn

我想检测特定列的单元格值是否已更改。

我的Datagridview名称是DGV_Products,它有6列。

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

我想仅在Quantity column单元格值更改而discount column值更改时才触发一些代码。我怎样才能完成任务?目前的代码,我试过的是什么;

 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

*抱歉英语不好。

3 个答案:

答案 0 :(得分:4)

哦,我得到了解决方案。 我使用e.ColumnIndex属性,我的问题已经解决了。

 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

感谢您的时间。

答案 1 :(得分:0)

以下为我工作

if (Visible && !(e.ColumnIndex == 0))
{
    phoneEdited = true;
    MessageBox.Show("A Phone entry has been entered");
}

答案 2 :(得分: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