将重复项添加到datagridview [英] Add duplicate to datagridview
问题描述
大家好.
我想知道如何:
与其在未绑定" Datgridview中添加重复项,还不如在项目旁边添加数量
如果在(产品A的列)产品"中找到了产品,则不得将其添加为新产品,而应更新(产品B列)数量" = 2
希望您能理解我的要求.
我正在使用以下代码查找重复的项目:
如果不存在,则将其添加到datagridview.
如果确实发现该项目已存在于datagridview中,则将其选中.
但是我现在需要它移动到同一行的下一列,并向Quatity添加另一个1 ....
它是一个简单的订购系统,正在构建中...
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]添加了代码块,将我的内容视为纯文本..."选项已禁用-OriginalGriff [/edit]
[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
推荐答案
感谢帮助人员,但我解决了它,它就像一个魅力..
请查看最终代码:
''如果您想尝试此代码,请创建以下代码:
按钮:btnAddData
文本框1:txtData1
文本框2:txtData2
Datagridview:dtgData
现在只需添加以下代码''
公共类frmtest
公共共享函数SearchGridValue(ByVal dtg作为DataGridView,ByVal ColumnName作为字符串,ByVal ValueToSearch作为字符串)作为布尔值
发现昏暗为布尔=假
Dim StringToSearch As String ="
值为String的Dim ValueToSearchFor = ValueToSearch.Trim.ToLower
昏暗的CurrentRowIndex为整数= 0
试试
CurrentRowIndex = dtg.CurrentRow.Index + 1
如果CurrentRowIndex> dtg.RowCount然后
CurrentRowIndex = dtg.RowCount-1
如果结束
对于i作为整数= CurrentRowIndex到dtg.RowCount-1
StringToSearch = dtg.Rows(i).Cells(ColumnName).Value.ToString.Trim.ToLower
如果StringToSearch.Contains(ValueToSearchFor)然后
将myCurrentCell设置为DataGridViewCell = dtg.Rows(i).Cells(ColumnName)
dtg.CurrentCell = myCurrentCell
找到=真实
如果结束
如果找到则
退出
如果结束
下一个
如果找不到,
将myFirstCurrentCell设置为DataGridViewCell = dtg.Rows(0).Cells(ColumnName)
dtg.CurrentCell = myFirstCurrentCell
如果结束
异常捕获
MsgBox("Error:"& ex.Message,MsgBoxStyle.Information)
结束尝试
找到退货
最终功能
私有Sub btnAddData_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理btnAddData.Click
试试
如果SearchGridValue(Me.dtgData,"Data1",Me.txtData1.Text)然后
MessageBox.Show(Me.txtData1.Text&已经在列表中!")
txtData1.Text ="
dtgData.CurrentCell = dtgData.CurrentRow.Cells(1)
dtgData.CurrentCell.Value =(dtgData.CurrentCell.Value)+(1)
退出子
如果结束
dtgData.Rows.Add(Me.txtData1.Text)
异常捕获
MsgBox(ex.Message)
结束尝试
结束子
结束类
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
私有子项btnAddData_Click(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理btnAddData.Click
试试
如果SearchGridValue(Me.dtgData,"Data1",Me.txtData1.Text)然后
MessageBox.Show(Me.txtData1.Text&已经在列表中!")
txtData1.Text ="
"这就是我现在添加到我的代码中的内容,它选择重复的条目,然后移到下一列,现在我只需要告诉它在当前值上添加"1" ..."
dtgData.CurrentCell = dtgData.CurrentRow.Cells(1)
退出子
如果结束
dtgData.Rows.Add(Me.txtData1.Text)
异常捕获
MsgBox(ex.Message)
结束尝试
结束子
结束类
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
i''m使用此代码在datagrid中搜索记录
i''m using this code in searching records in datagrid
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
*注意:0是字段ex的列号.产品
希望对您有所帮助!
*Note: 0 is the column number of the field ex. Product
Hope it helps!
这篇关于将重复项添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!