Click here to Skip to main content
15,906,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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......


VB
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]
Posted
Updated 20-Feb-12 21:25pm

Thanks for the help guys but I solved it and it works like a charm..

Please see final code:

'If you want to try this code create the following:
Button: btnAddData
Textbox1: txtData1
Textbox2: txtData2
Datagridview: dtgData

Now just add the following code'

Public Class frmtest




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 = ""


dtgData.CurrentCell = dtgData.CurrentRow.Cells(1)

dtgData.CurrentCell.Value = (dtgData.CurrentCell.Value) + (1)



Exit Sub
End If
dtgData.Rows.Add(Me.txtData1.Text)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub


End Class
 
Share this answer
 
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 = ""

'This is what i added into my code now and it selects the duplicate entry and then shifts to the next column where I now just need to tell it to add a "1" to the current value...'

dtgData.CurrentCell = dtgData.CurrentRow.Cells(1)

Exit Sub
End If
dtgData.Rows.Add(Me.txtData1.Text)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
 
Share this answer
 
i'm using this code in searching records in datagrid

VB
for x = 0 to dgt.rowcount -1
   if dgt.Item(0, x).Value = StringToSearch then
      dgt.Item(1, x).Value = 2 'modify the quantity field to whatever value you want
      found = true
   end if
next x


*Note: 0 is the column number of the field ex. Product

Hope it helps!
 
Share this answer
 
v2
Comments
mariovd 23-Feb-12 2:35am    
Thank you chaun0308
this is great. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900