windows forms datagridview compare new row against entire grid

Posted on 2011-10-04
Last Modified: 2012-05-12
I have a datagridview on my windows form.  I have 2 columns.  
1st column is upc and 2nd column is item description.
if a user puts in a upc, it then goes to the db and gets the item description.
i am now doing some error checking.  so if a user puts in the same upc a second time i want a msgbox to pop up saying duplicate entry.
i am doing this error checking in the   Datagridview1_CellValueChanged (is this the best place?)

here is what i have and it isnt working well at all.

  Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

        If Not e.RowIndex = "-1" Then

            Dim strBuild As New System.Text.StringBuilder()
            Dim strSQLString As String
            Dim sqlcmdcnt As SqlCommand
            Dim drIV00200 As SqlDataReader


                lRowIndex = DataGridView1.CurrentRow.Index
                lRowError = False

                If e.ColumnIndex = 0 Then

                    Me.DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty

                    'If any values entered/scanned on the line
                    If (DataGridView1(0, DataGridView1.CurrentRow.Index).EditedFormattedValue.ToString.Trim <> "") Then

                        'Check for duplicate entries
                        For Each row As DataGridViewRow In DataGridView1.Rows
                            If sender.text.Trim.ToString = sender.EditingControl.text.trim.ToString() Then

                                DataGridView1.Rows(e.RowIndex).ErrorText = "Duplicate entry - " + DataGridView1(0, DataGridView1.CurrentRow.Index).EditedFormattedValue.ToString().Trim
                                Me.DataGridView1.EditingControl.Text = ""
                                '''  e.Cancel = True   ? do i put a remove command here also?

                                Exit Sub
                            End If

Question by:purdyra1
    LVL 83

    Expert Comment

    Are you using a DataTable? If so, you can do a RowFilter on it

    dTable.DefaultView.RowFilter = "UPC=" & newUPCvalue

    If dTable.DefaultView.Count > 0 Then
       'Duplicate value.
    End If

    Accepted Solution

    Thanks for the reply.  I have figured it out and here is what I did.
    I went back to the CellValidating and did my code there.

    I check and see if the data coming across is blank or not and then if so,
          e.Cancel = True
          DataGridView1.Rows(e.RowIndex).ErrorText = "This field cannot be empty or press Escape"
    I also added:
        ' Clear the row error in case the user presses ESC.  
        Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
        Handles DataGridView1.CellEndEdit

            dataGridView1.Rows(e.RowIndex).ErrorText = String.Empty

        End Sub

    Author Closing Comment

    I tried my best and this seems to work, i don't do much windows forms stuff, mainly
    LVL 15

    Expert Comment

    the CellValueChanged don´t has e.Cancel support.

    You can use CellValidating event.

        Private Sub dgv_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgv.CellValidating
            If dgv.IsCurrentCellDirty = False Then Return
                nv = DurCpm.Parse(e.FormattedValue)
            Catch ex As Exception
                e.Cancel = True
            End Try

    - I use dgv.IsCurrentCellDirty to avoid validate a not changed Cell.
    - Use e.FormattedValue to get the user typed value. Cell value is not set yet.
    - You can use e.cancel when value is not valid.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now