Reputation:
I am trying to check each row in the DataGridView whenever I add an Item in order to avoid duplicates. But my code only allows me to check the first data I added.
Here is my code:
For Each row In BarcodePrintListGrid.Rows
If Label44.Text = row.Cells("Barcode ID").Value Then
MetroMessageBox.Show(Me, "Item Already Added", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Label47.Text = "None"
Label44.Text = "None"
Label42.Text = "None"
Label31.Text = "None"
Label40.Text = "0"
TextBox1.Clear()
Exit For
Else
BarcodePrintListGrid.Rows.Add(Label47.Text, Label44.Text, Label42.Text, Label31.Text, Label40.Text, 1)
MetroMessageBox.Show(Me, "Item Added to List", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Label47.Text = "None"
Label44.Text = "None"
Label42.Text = "None"
Label31.Text = "None"
Label40.Text = "0"
TextBox1.Clear()
Exit For
End If
Next
Upvotes: 0
Views: 6454
Reputation: 2180
You have to wait until the loop has gone all the rows of your DataGridView
before deciding to add the row to it or not.
Try with this code :
Dim test As Boolean = False
For Each row In BarcodePrintListGrid.Rows
If Label44.Text = row.Cells("Barcode ID").Value Then
test=true
Exit For
End If
Next
if test=false then
BarcodePrintListGrid.Rows.Add(Label47.Text, Label44.Text, Label42.Text, Label31.Text, Label40.Text, 1)
MetroMessageBox.Show(Me, "Item Added to List", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
else
MetroMessageBox.Show(Me, "Item Already Added", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
end if
Label47.Text = "None"
Label44.Text = "None"
Label42.Text = "None"
Label31.Text = "None"
Label40.Text = "0"
TextBox1.Clear()
Upvotes: 1