Avatar of purdyra1
purdyra1Flag for United States of America

asked on 

windows forms datagridview compare new row against entire grid

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

            Try

                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
                        Next

etc....
Visual Basic.NETVisual C++.NET

Avatar of undefined
Last Comment
x77
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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
ASKER CERTIFIED SOLUTION
Avatar of purdyra1
purdyra1
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of purdyra1
purdyra1
Flag of United States of America image

ASKER

I tried my best and this seems to work, i don't do much windows forms stuff, mainly asp.net.
Avatar of x77
x77
Flag of Spain image

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
        Try
            nv = DurCpm.Parse(e.FormattedValue)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            e.Cancel = True
            Return
        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.
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo