[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

windows forms datagridview compare new row against entire grid

Posted on 2011-10-04
4
Medium Priority
?
518 Views
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

            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
Comment
Question by:purdyra1
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36919482
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
 

Accepted Solution

by:
purdyra1 earned 0 total points
ID: 36919714
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
 

Author Closing Comment

by:purdyra1
ID: 36941256
I tried my best and this seems to work, i don't do much windows forms stuff, mainly asp.net.
0
 
LVL 15

Expert Comment

by:x77
ID: 36919731
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

873 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