【发布时间】:2017-03-24 20:46:22
【问题描述】:
我有一个这样的DataGridView:
Quantidade 列可由用户更改,其他为只读。我想出了这段代码,如果用户在DataGridView 上手动更改Quantidade 的值,它会检查数据库以查看是否有足够的库存。因此,如果用户输入的值小于库存总值,它会正常更改,但我的问题是,如果用户输入的值大于库存值,我希望 DataGridViewCell 返回到更改之前的值由用户。
关于如何做到这一点的任何想法?
这是事件的代码:
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
'this part is to check the total of the product in the db
Dim Produtoid As String = DataGridView1.Rows(e.RowIndex).Cells(0).Value
Dim tabelaqndDisponivel As DataTable = CAD.GetData("SELECT quantidadeExistenteProduto FROM Produto where idProduto = " & Produtoid)
'qntDisponivel is a integer that holds the total quantity of the product in the db
Dim qntDisponivel As Integer = tabelaqndDisponivel.Rows(0).Item(0)
If DataGridView1.Rows(e.RowIndex).Cells(2).Value <= qntDisponivel Then
'inserts normally
Else
'now here the value on cell "quantidade" should revert
End If
End Sub
请注意,这个DataGridView 非常简单。它从ComboBox Produto 中获取值,并从TextBox Quantidade 中获取文本
【问题讨论】:
-
好吧,我想我知道该怎么做了。因此,如果单元格验证保存“用户更改之前的值”,我可以将其保存在一个变量中,然后在 CellValueChanged 中,否则我可以将单元格值恢复为该变量
标签: vb.net datagridview