>Private Sub dg_CellValidating(ByVal sender As Object, _ > ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dg.CellValidating > > > Dim strErrMsg As String > For i = 0 To dg.Rows.Count - 1 > dg.Rows(i).ErrorText = "" > Next > > Try > With dg > If e.ColumnIndex = 0 Then > If CStr(e.FormattedValue).Length = 0 Then > strErrMsg = "a must entry!" > .Rows(e.RowIndex).ErrorText = strErrMsg > e.Cancel = True > MsgBox("a must entry!", 48, "warning") > Else > Dim htTable As Hashtable = New Hashtable > Dim iRow As Integer > Dim sProductID As String > Try > .Rows(iRow).ErrorText = "" > > For iRow = 0 To .Rows.Count - 1 > sProductID = _ > Microsoft.VisualBasic.Left( _ > CStr(.Rows(iRow).Cells(0). _ > EditedFormattedValue) & Space(4), _ > 4) > htTable.Add(sProductID, iRow) > Next > Catch ex As Exception > strErrMsg = "a must not duplicate." > .Rows(e.RowIndex).ErrorText = strErrMsg > e.Cancel = True > MsgBox("a must not duplicate!", 48, "warning") > End Try > End If > End If >end sub > >It looks as if you want to prevent duplicate values in any column. Something like this might be a starting point:
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { DataGridView dg = sender as DataGridView; string newValue = e.FormattedValue.ToString(); if (newValue != string.Empty) { var v = from DataGridViewCell x in dg.Rows[e.RowIndex].Cells where (x.ColumnIndex != e.ColumnIndex && x.Value != null && x.FormattedValue.ToString()==newValue) select x.DataGridView.Columns[x.ColumnIndex].HeaderText; foreach (string s in v) { MessageBox.Show("Duplicate in " + s); e.Cancel = true; } } }C# I'm afraid but you can run it through a converter