Hi Guys.
I would like to know how to:
Instead of adding a duplicate to a “Unbound” Datgridview it must rather add to the quantity next to the item
if it finds the Item in (Column A) “Product” .. it must not add it as a new item but rather Update (Column B) “Quantity” = 2
I hope you will be able to understand what Im asking.
I am using the following code to find the duplicate Item:
and if it does not exist it adds it to the datagridview.
if it does find the item already exists in datagridview then it will select it.
But I need it now to move to next column in same row and add another 1 to the Quatity.......
Its a simple ordering system im building......
Public Shared Function SearchGridValue(ByVal dtg As DataGridView, ByVal ColumnName As String, ByVal ValueToSearch As String) As Boolean
Dim Found As Boolean = False
Dim StringToSearch As String = ""
Dim ValueToSearchFor As String = ValueToSearch.Trim.ToLower
Dim CurrentRowIndex As Integer = 0
Try
CurrentRowIndex = dtg.CurrentRow.Index + 1
If CurrentRowIndex > dtg.RowCount Then
CurrentRowIndex = dtg.RowCount - 1
End If
For i As Integer = CurrentRowIndex To dtg.RowCount - 1
StringToSearch = dtg.Rows(i).Cells(ColumnName).Value.ToString.Trim.ToLower
If StringToSearch.Contains(ValueToSearchFor) Then
Dim myCurrentCell As DataGridViewCell = dtg.Rows(i).Cells(ColumnName)
dtg.CurrentCell = myCurrentCell
Found = True
End If
If Found Then
Exit For
End If
Next
If Not Found Then
Dim myFirstCurrentCell As DataGridViewCell = dtg.Rows(0).Cells(ColumnName)
dtg.CurrentCell = myFirstCurrentCell
End If
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Information)
End Try
Return Found
End Function
Private Sub btnAddData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddData.Click
Try
If SearchGridValue(Me.dtgData, "Data1", Me.txtData1.Text) Then
MessageBox.Show(Me.txtData1.Text & " Already in the list!")
txtData1.Text = ""
Exit Sub
End If
dtgData.Rows.Add(Me.txtData1.Text)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]