我想检测特定列的单元格值是否已更改。
我的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
*抱歉英语不好。
答案 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