我正在CellValidated事件和CellEndEdit事件上验证我的DataGridView单元。当我按Tab键并且光标移到下一个单元格时,会触发错误并显示错误消息。
我想知道如何将单元格移至错误列和错误行(将光标停在错误单元格上)。
我的代码就是这样。
Private Sub dgDataGrid _CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgDataGrid.CellEndEdit
If dgPettyCashEnter.Columns(e.ColumnIndex).Name = "Account" Then ' Validating Account Number
Dim odcCommand As OleDbCommand = New OleDbCommand("<Stored Procedure>", <Connection>)
' Stored Procedure Process
Dim <odrDataReader> As OleDbDataReader = odcCommand.ExecuteReader()
Do While odrDataReader.Read
If CInt(odrDataReader.GetValue(0)) = 0 Then
dgDataGrid.Rows(e.RowIndex).ErrorText = "Account No Not Found. Invalid Account No."
intCol = e.ColumnIndex
intRow = e.RowIndex
booErrorFire = True
Else
dgDataGrid.Rows(e.RowIndex).ErrorText = ""
booErrorFire = False
End If
Loop
End If
End Sub
Private Sub dgDataGrid _CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgDataGrid.CellValidated
Try
If dgDataGrid.Columns(e.ColumnIndex).Name = "Account" Then ' Validating Account Number
If CStr(dgDataGrid (2, e.RowIndex).Value).Length > 6 Or CStr(dgDataGrid (2, e.RowIndex).Value).Length = 0 Then ' Account Code Length Invalid
dgDataGrid.Rows(e.RowIndex).ErrorText = "Account Code Length Invalid"
intCol = e.ColumnIndex
intRow = e.RowIndex
booErrorFire = True
Else ' Account Code Length Valid
dgDataGrid.Rows(e.RowIndex).ErrorText = ""
booErrorFire = False
End If
Else
dgDataGrid.Rows(e.RowIndex).ErrorText = ""
booErrorFire = False
End If
Catch ex As NullReferenceException
If dgDataGrid.Columns(e.ColumnIndex).Name = "Account" Then
dgDataGrid.Rows(e.RowIndex).ErrorText = "Account Code Length Invalid"
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub