I have a DataGridView control on my form. It has 6 columns and 6 rows (which will never change). When a user enters data in a cell under any column, I want to make sure they fill in the rest of the cells for that row. So basically if they put data in row 0 – column 0, I want to ensure that row 0 – column 1, row 0 – column 2 and so on… have data in them. I need this for validation reasons before this will be committed to the database. If the fields are not all filled in for that row, I want to display a message containing the rows that need to be fixed.
Any help is greatly appreciated!
Here’s an update, I have figured out what needed to be done.
Private Sub ValidateYear()
Dim oInvYear As New Collection
Dim oErrorMsg As New System.Text.StringBuilder
Dim blnErrFound As Boolean = False
'Loop through year column and check for number, if blank skip'
For i As Integer = 0 To dgvIntervals.Rows.Count - 1
If Not String.IsNullOrEmpty(dgvIntervals.Rows(i).Cells(4).Value) Then
If Not IsNumeric(dgvIntervals.Rows(i).Cells(4).Value) Then
oInvYear.Add(i + 1)
blnErrFound = True
End If
End If
Next
'If errors found, lets append them to our message'
If blnErrFound Then
oErrorMsg.Append("PLEASE FIX ERRORS BELOW BEFORE PROCEEDING")
oErrorMsg.AppendLine("")
oErrorMsg.Append(vbCrLf)
'Get our year count errors'
If oInvYear.Count > 0 Then
oErrorMsg.Append("* Year must be a number- ")
oErrorMsg.Append("Line(s): ")
For i As Integer = 1 To oInvYear.Count
If i >= 2 Then
oErrorMsg.Append(", ")
End If
oErrorMsg.Append(oInvYear.Item(i).ToString)
Next
oErrorMsg.Append(vbCrLf)
End If
'Show them to our user'
MsgBox(oErrorMsg.ToString)
End Sub
Use the CellValidating or RowValidating events of the DataGridView control to validate the data that has been entered by the user.