Datagridview cellvalidating效果不佳 [英] Datagridview cellvalidating is not working perfectly
问题描述
我有3列
*商品名称
*现有数量
*转帐数量
项目名称和手头的数量显示给用户。如果他试图输入一个超过他手头数量的数字;我需要一个消息框来弹出...
然而事件并没有触发确切的我想要的时间。它允许我在没有弹出消息的情况下离开行。
我尝试了什么:
<私有子DataGridView1_CellValidating(ByVal发送者为对象,ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs)处理DataGridView1.CellValidating
使用DataGridView1。行(e.RowIndex)
如果e.ColumnIndex = 2那么''这是要转移的数量的col索引
如果.Cells(2).Value> ; .Cells(1).Value Then
MessageBox.Show(你的数量不足,输入错误,MessageBoxButtons.OK,MessageBoxIcon.Error)
e.Cancel = True
结束如果
结束如果
结束
end sub
I have 3 columns
* Item Name
* Quantity on Hand
* Quantity to transfer
the item name and the qty on hand is diplayed to the user . If he tries to input a number which is more than the Qty he has on Hand; i need a messageBox to pop up...
However the event is not firing the exact time I want. It allows me to leave the row without the message popping up.
What I have tried:
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
With DataGridView1.Rows(e.RowIndex)
If e.ColumnIndex = 2 Then '' this is the col index for qty to be transfered
If .Cells(2).Value > .Cells(1).Value Then
MessageBox.Show("You don't Have sufficient quantity ", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
End If
End If
End With
end sub
推荐答案
Private Sub DataGridView1_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
Dim transferredQTY, onHandQTY As Double
With DataGridView1.Rows(e.RowIndex)
Try
transferredQTY = Convert.ToDouble(.Cells(2).Value)
Catch ex As Exception
transferredQTY = 0
End Try
Try
onHandQTY = Convert.ToDouble(.Cells(1).Value)
Catch ex As Exception
transferredQTY = 0
End Try
If transferredQTY > onHandQTY Then
MessageBox.Show("You don't Have sufficient quantity ", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
End If
End With
End Sub
谢谢JOVY ..我想我发现了什么问题。该值尚未写入单元格,因此我使用了从事件中传递的值并解决了我的问题
Thanks J.O.V.Y... I think i found out what was the problem. The value wasn't written to the cell yet, so i used the value that is being passed from the event and that solved my problem
If Convert.ToInt32(e.FormattedValue) > Convert.ToInt32(.Cells(1).Value) Then
这篇关于Datagridview cellvalidating效果不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!