在DataGridView的特定列中输入关键事件

时间:2013-08-15 12:39:44

标签: vb.net datagridview

enter image description here

在此DataGridView中,如果我在DriverID列中按 Enter 键,我想检查是否仅输入DriverID列。如果我输入DriverID列,我想显示一条消息。

如何处理 Enter 按键事件?

我的代码如下所示:

If e.ColumnIndex = 2 Then
    If DGVall.CurrentRow.Cells(2).Value = "" Then
        MessageBox.Show("Please Enter Driver ID")
        Exit Sub
    End If
End If

但是当我按下DriverId列中的 Enter 键时,它应该会发生

我该怎么做?

1 个答案:

答案 0 :(得分:0)

你的问题有点模糊,但据我所知:你想强迫人们填写第一个单元格,如果没有,你想要一条消息......

首先,你需要CellValidating事件......

你会得到类似的东西;

    Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    If e.ColumnIndex = 0 Then
        If DataGridView1(e.ColumnIndex, e.RowIndex).Value Is Nothing AndAlso e.FormattedValue.ToString.Trim = "" Then
            MsgBox("You have to fill this in...")
        End If
    End If
End Sub
相关问题