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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25020381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:02:22  来源:igfitidea点击:

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

vb.netdatagridviewdatagridviewcolumn

提问by Muhammad Saqib

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

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

My Datagridview name is DGV_Productsand it has 6 columns.

我的 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

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

我只想在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

*Apologies for bad english.

*抱歉英语不好。

回答by Muhammad Saqib

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

哦,我得到了解决方案。我使用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

Thank you for your time.

感谢您的时间。

回答by Jeff

The following worked for me

以下对我有用

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

回答by Raj Kumar

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