Datagridview CellValidating不能完美运行

时间:2016-08-11 20:39:03

标签: vb.net

我有3列

  • 项目名称

  • 手头数量

  • 转帐数量

项目名称和手头的数量显示给用户 。如果他试图输入一个超过他手头数量的数字;我需要一个消息框来弹出...

这是我试过的

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

然而事件并没有触发我想要的确切时间。它允许我在没有弹出消息的情况下离开行。

1 个答案:

答案 0 :(得分:0)

该值尚未写入单元格,因此请使用从事件传递给您的值:

If Convert.ToInt32(e.FormattedValue) > Convert.ToInt32(.Cells(1).Value) Then

旁注:在代码中设置Option Strict On,因为Value和FormattedValue返回对象