DataGridView CellValidating在新行上返回空字符串

时间:2015-12-22 09:32:42

标签: mysql vb.net datagridview

我的DataGridView表示可以编辑的MySQL表。但是,第一列需要遵循我需要验证的严格模式。我使用以下代码来执行此操作:

Private Sub dg_Temp_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dg_Temp.CellValidating
    Dim strActiveCell = dg_Temp(e.ColumnIndex, e.RowIndex).Value.ToString
    If e.ColumnIndex = 0 Then
        If Regex.IsMatch(strActiveCell, "M[0-9]{3}[a-z]{1}[A-Z]{1}") = False Then
            e.Cancel = True
            MsgBox("Invalid material id structure." & Chr(13) & "Only the following syntax is legal: M000aA" &
                   Chr(13) & "Entered: " & strActiveCell)
        End If
    ElseIf strActiveCell = vbNullString Then
        dg_Temp(e.ColumnIndex, e.RowIndex).Value = ""
    End If
End Sub

但是,在编辑现有行时,一切正常。但是在创建新行时,dg_Temp(e.ColumnIndex, e.RowIndex).Value.ToString不返回任何内容。我错过了什么?

1 个答案:

答案 0 :(得分:0)

使用事件参数e.FormattedValue获取正在验证的单元格的当前值。

g_Temp(e.ColumnIndex, e.RowIndex).Value似乎直到CellValidating事件之后才设置。