Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub MyForm_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles myDGV.CellValidating
- Dim dgv As DataGridView = CType(sender, DataGridView)
- Select Case dgv.Columns(e.ColumnIndex).Name
- Case "uniqueColumn"
- ' Validate that the values in our unique column are unique.
- For i As Integer = 0 To dgv.RowCount - 1
- If i <> e.RowIndex Then
- ' Here i != j, so compare to the value...
- If e.FormattedValue = dgv.Rows(i).Cells(e.ColumnIndex).FormattedValue Then
- e.Cancel = True
- 'dgv.ShowRowErrors = True
- 'dgv.Rows(e.RowIndex).ErrorText = "Data in the unique column must be unique"
- End If
- End If
- Next 'i
- Case Else
- ' Perform no validation.
- End Select
- End Sub
- e.Cancel = True
Add Comment
Please, Sign In to add comment