How disallow duplicate items in datagrid view?

trialer

Well-known member
Joined
Oct 4, 2010
Messages
64
Programming Experience
1-3
im making of a program that will move items from datagridview1 to datagridview2.
if the selected row to be move from datagridview have already in the in the datagridview2 it will do nothing
but if it doesn't have in datagridview2 it will add to its items.

here's my code but have some errors
VB.NET:
str1 = DataGridView1.CurrentRow.Cells("Description1").Value
        If DataGridView2.Rows.Count > 0 Then
            With DataGridView2
                For Each row As DataGridViewRow In .Rows
                    str2 = row.Cells("Description2").Value
                    If str1 = str2 Then
                        MsgBox(str1 + " = " + str2)
                    Else
                        MsgBox(str1 + " <> " + str2)
                        With DataGridView2.Rows
                        .Add(DataGridView1.CurrentRow.Cells("Description1").Value)
                        End With
                    End If
                Next
            End With
            Exit Sub
        ElseIf DataGridView2.Rows.Count < 1 Then
            With DataGridView2.Rows
                .Add(DataGridView1.CurrentRow.Cells("Description1").Value)
            End With
        End If
 
Last edited:
Hi trialer,

I think there are two problems with your code.

First, you actually need two loops; you need one to check dgv2 for duplicates, but you also need an outer loop to go through all the rows in dgv1. The second problem is that when you loop through the rows using For Each, that does not actually change the CurrentRow. So your original code was always working with the same current row.

Do you know how to use the debugger? Just press F9 on the line where you want the program to stop. Then run the program, and when it stops, you can then execute one line at a time with F8, and you can see the values changing.

Ok, so here my suggested solution. I tried to change as little as possible:

Dim indgv2 As Boolean
For dgv1row As Integer = 0 To DataGridView1.Rows.Count - 1
    If DataGridView2.Rows.Count > 0 Then
        indgv2 = False
        str1 = DataGridView1.Rows(dgv1row).Cells("Description1").Value
        For dgv2row As Integer = 0 To DataGridView2.Rows.Count - 1
            str2 = DataGridView2.Rows(dgv2row).Cells("Description2").Value
            If str1 = str2 Then
                indgv2 = True
                Exit For
            End If
        Next
        If Not indgv2 Then
            DataGridView2.Rows.Add(DataGridView1.Rows(dgv1row).Cells("Description1").Value)
        End If
    Else
        DataGridView2.Rows.Add(DataGridView1.Rows(dgv1row).Cells("Description1").Value)
    End If
Next


There is a boolean called indgv2, which is to signal if the value in dgv1 is found in dgv2. That way, if you find the value, you set it to TRUE and exit the for loop. You are finished checking even if you are not finished looping. But if you don't find it, you end the loop normally, and the boolean variable remains FALSE.

So at the end of the inner for loop, if you don't find the value, you add it. But instead of CurrentRow, we now loop with integers (dgv1row and dgv2row) that will access the Rows collection.

Ok, hope that helps. :)
 
Robert_SF - I think the OP stated he wanted to move a single row across and check if it exists in the target gridview, which on a quick scan he is attempting.

trialer - what are the error messages you're getting?
 
Thanks for helping me to solve this.

here's my new code for now.

VB.NET:
[COLOR=#0000ff]
        Dim[/COLOR] indgv2 As [COLOR=#0000ff]Boolean[/COLOR]
        [COLOR=#0000ff]If [/COLOR]DataGridView2.Rows.Count > 0 [COLOR=#0000ff]Then[/COLOR]
            indgv2 =[COLOR=#0000ff] False[/COLOR]
            str1 = DataGridView1.CurrentRow.Cells([COLOR=#a52a2a]"Description1"[/COLOR]).Value
            [COLOR=#0000ff]For [/COLOR]dgv2row [COLOR=#0000ff]As Integer [/COLOR]= 0 [COLOR=#0000ff]To [/COLOR]DataGridView2.Rows.Count - 1
                str2 = DataGridView2.Rows(dgv2row).Cells([COLOR=#a52a2a]"Description2"[/COLOR]).Value
                [COLOR=#0000ff]If [/COLOR]str1 = str2 [COLOR=#0000ff]Then[/COLOR]
                    indgv2 = [COLOR=#0000ff]True[/COLOR]
                   [COLOR=#0000ff] Exit For
                End If
            Next[/COLOR]
         [COLOR=#0000ff]   If Not[/COLOR] indgv2 [COLOR=#0000ff]Then[/COLOR]
                With DataGridView2.Rows
                    .Add(DataGridView1.CurrentRow.Cells([COLOR=#a52a2a]"Description1"[/COLOR]).Value)
                [COLOR=#0000ff]End With
            End If
        Else[/COLOR]
            DataGridView2.Rows.Add(DataGridView1.CurrentRow.Cells([COLOR=#a52a2a]"Description1"[/COLOR]).Value)
       [COLOR=#0000ff] End If[/COLOR]
 
Last edited:
Back
Top