• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

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....
0
purdyra1
Asked:
purdyra1
  • 2
1 Solution
 
CodeCruiserCommented:
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
0
 
purdyra1Author Commented:
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
0
 
purdyra1Author Commented:
I tried my best and this seems to work, i don't do much windows forms stuff, mainly asp.net.
0
 
x77Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now