Dim blnErrorsExist As Boolean = False Dim strMsg As String = "" Dim strValue As String = "" Dim strSQL As System.Text.StringBuilder Dim cm As CurrencyManager = _ CType(DataGrid.BindingContext(DataGrid.DataSource), CurrencyManager) Dim intRowCount As Integer = cm.Count - 1 Dim intRowIndex As Integer = 0 Dim intRows As Integer = 0 Dim intColIndex As Integer Dim intColCount As Integer = 1 ' ' Loop through the datagrid rows. ' For intRowIndex = 0 To intRowCount ' ' Initialize the filter string. Trim values so "JOE" matches " JOE ". ' strSQL = "Trim(Cust_Name)='?0' AND (Trim(Cust_Office)='?1' ?2)" ' ' Loop through the row's columns. The key to the Customer table is ' Cust_Name. If the key consisted of more than one column you could ' easily expand this technique. ' For intColIndex = 0 To intColCount ' ' Get the datagrid cell's value and trim it. ' strValue = DataGrid(intRowIndex, intColIndex).ToString().Trim() Select Case intColIndex Case 0 ' ' Customer name is required. ' If strValue = "" Then blnErrorsExist = True strMsg = "Missing Customer Name." Exit For End If ' ' Build the filter SQL. ' strSQL = strSQL.Replace("?0", strValue) Case 1 ' ' The Customer Office can be blank or Null. The filter string ' must catch this and treat the following 2 rows as duplicates: ' Cust Name Cust Office ' JOE blank ' JOE null ' strSQL = strSQL.Replace("?1", strValue) If aValue = "" Then strSQL = strSQL.Replace("?2", " OR Cust_Office IS Null") Else strSQL = strSQL.Replace("?2", "") End If End Select Next If blnErrorsExist Then Exit For ' ' Look for duplicate rows by applying a filter ' and checking the number of rows. ' DS.Tables("Customers").DefaultView.RowFilter = strSQL intRows = DS.Tables("Customers").DefaultView.Count DS.Tables("Customers").DefaultView.RowFilter = "" If intRows > 1 Then blnErrorsExist = True strMsg = "Duplicate row found. The Customer Name" & vbCrLf & _ "and Customer Office must be unique." Exit For End If Next ' ' If an error was found go to the bad row and issue a message. ' If blnErrorsExist Then DataGrid.Select(intRowIndex) ' ' See text below. ' DataGrid.ScrollToRow(intRowIndex) DataGrid.CurrentRowIndex = intRowIndex MessageBox.Show(strMsg,"Error Saving Data", _ MessageBoxButtons.OK, MessageBoxIcon.Error) Return End If