Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How to scan DataGridView Column

$
0
0

Hi folks,

I have function that checks for duplicate values in a datagridview column name acctid. There must no duplicate values in this column.

So I wrote this function to scan this column and return true/false depending on whether there duplicates or not.  I placed this function in the CellValueChanged event of the datagridview.

My problem is that the function is able to return the right value but it also throws an exception with the message: 'Object reference not set to an instance of an object'. I have shown my code blocks below

Private Function chkDuplicateInc(b As DataGridView) As Boolean
        Dim intCtr, intCur As Integer
        'Dim acc As String = b.Rows(intRow).Cells(20).Value.ToString '
        Dim acc As String = CType(incomeBindingSource.Current, DataRowView).Row.Item("acctid").ToString()
        'chk for duplicates
        For intCtr = 0 To b.Rows.Count - 1
            If b.Rows(intCtr).Cells(20).Value.ToString = acc Then
                'the acc exists, show message
                intCur += 1
                If intCur > 1 Then
                    Exit For
                End If
            End If
        Next
        If intCur > 1 Then
            Return True
        Else
            Return False
        End If
End Function

Private Sub DataGridView2_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellValueChanged If (incomeBindingSource.Count <> 0) AndAlso (CType(incomeBindingSource.Current, DataRowView).Row.Item("acctid").ToString <> String.Empty) Then If chkDuplicateInc(DataGridView2) Then 'the acc exists, show message MessageBox.Show("Please duplicate entries are not allowed", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error) incomeBindingSource.CancelEdit() Exit Sub

End If End If End sub




Thanks so much in advance


PA




Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>