如何控制列表视图中的重复数据条目 [英] How to control duplicate data entry in a listview
本文介绍了如何控制列表视图中的重复数据条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI伙计们,
我用这段代码检查列表视图中是否存在添加的项目。但是它不起作用。你快看一下吗?
< span class =code-keyword>私有 Sub CheckDuplicates()
Dim itemI 作为 ListViewItem
Dim itemJ As ListViewItem
Dim ok As Integer
对于 i 作为 Integer = ListView1.Items.Count - 1 0 步骤 -1
itemI = ListView1.Items(i)
对于 j 作为 整数 = i + 1 ListView1.Items.Count - 1 步骤 1
itemJ = ListView1.Items(j)
If itemI.SubItems( 0 )。Text = itemJ.SubItems( 0 ).Text AndAlso _
itemI.SubItems( 1 )。Text = itemJ.SubItems( 1 )。文本 AndAlso _
itemI.SubItems( 1 )。Text = itemJ.SubItems( 2 )。文本 AndAlso _
itemI.SubItems( 1 )。Text = itemJ.SubItems( 3 )。文本 AndAlso _
itemI。 SubItems( 1 )。Text = itemJ.SubItems( 4 )。文本 AndAlso _
itemI.SubItems( 1 )。Text = itemJ.SubItems( 5 )。文本 AndAlso _
itemI.SubItems( 1 )。 = itemJ.SubItems( 6 )。文本 AndAlso _
itemI.SubItems( 1 )。文本= itemJ.SubItems( 7 )。文本 AndAlso _
itemI.SubItems( 2 )。text = itemJ.SubItems( 8 ).Text 然后
' 找到重复的项目。
ok = MsgBox( 添加了重复的项目,请删除新项目,vbCritical + vbOKCancel,_
< span class =code-string> 桥梁建造成本估算)
如果 ok = vbOK 然后
ListView1.Items.Remove(itemJ)
结束 如果
退出 对于
结束 如果
下一步 j
下一步 i
结束 Sub
解决方案
有几个问题:
1. itemI的SubItem索引是没有增加。见下文
如果 itemI.SubItems( 0 )。Text = itemJ.SubItems( 0 )。文本 AndAlso _
itemI。 SubItems( 1 )。Text = itemJ.SubItems( 1 )。文本 AndAlso _
itemI.SubItems( 2 )。Text = itemJ.SubItems( 2 )。文本 AndAlso _
itemI.SubItems( 3 )。 = itemJ.SubItems( 3 )。文本 AndAlso _
itemI.SubItems( 4 )。文本= itemJ.SubItems( 4 )。文本 AndAlso _
itemI.SubItems( 5 )。文本= itemJ.SubItems( 5 )。文本 AndAlso _
itemI.SubItems( 6 )。Text = itemJ.SubItems( 6 )。文本 AndAlso _
itemI.SubItems( 7 )。文本= itemJ.SubItems( 7 )。文本 AndAlso _
itemI.SubItems( 8 )。文本= itemJ.SubItems( 8 )。文本然后
' 找到重复的项目。
2.当您删除
项目时,所有项目的索引号更高的索引号减1。
3.我不确定第二个For
语句。我会用这个:对于 j As Integer = ListView1.Items.Count - 1 to 0 Step -1
4.需要添加一个检查以确保您没有将相同的条目与自身进行比较。
如果 i<> j 然后
如果 itemI.SubItems( 0 )。文本= itemJ.SubItems( 0 )。文本 AndAlso _
itemI.SubItems( 1 )。Text = itemJ.SubItems( 1 )。 AndAlso _
itemI.SubItems( 2 )。Text = itemJ.SubItem s( 2 )。文本 AndAlso _
itemI.SubItems( 3 )。Text = itemJ.SubItems( 3 )。文本 AndAlso _
itemI.SubItems( 4 )。Text = itemJ.SubItems( 4 )。 AndAlso _
itemI.SubItems( 5 )。Text = itemJ.SubItems( 5 )。文本 AndAlso _
itemI.SubItems( 6 )。Text = itemJ.SubItems( 6 )。文本 AndAlso _
itemI.SubItems( 7 )。text = itemJ.SubItems( 7 )。文本 AndAlso _
itemI.SubItems( 8 )。Text = itemJ .SubItems( 8 )。文本然后
' 找到重复的项目
ok = MsgBox( 添加了重复项,请删除新项,vbCritical + vbOKCancel,_
桥梁建设成本估算)
如果 ok = vbOK 那么
ListView1.Items.Remove(itemJ)
结束 如果
结束 如果
在将项目添加到ListView控件之前检查重复。
Dim itemJ As ListViewItem
Dim bFound As 布尔值 = 错误
对于 j As 整数 = 0 至 ListView1.Items.Count - 1
itemJ = ListView1.Items(j)
如果 payItem.ToString()= itemJ.SubItems( 0 )。文本 AndAlso _
subpayItem.ToString()= itemJ.SubItems( 1 )。文本 AndAlso _
subItem = itemJ.SubItems( 2 )。文本 AndAlso _
unit = itemJ.SubItems( 3 )。文本 AndAlso _
bridgeType = itemJ.SubItems( 4 )。文本 AndAlso _
span = itemJ.SubItems( 5 )。文本 AndAlso _
quantity.ToString()= itemJ.SubItems( 6 )。文本 AndAlso _
Rate.ToString()= itemJ.SubItems( 7 )。文本 AndAlso _
amount.ToString = itemJ.SubItems( 8 )。文本然后
' 找到重复的项目。
bFound = True
退出 对于
结束 如果
下一步
如果 bFound 那么
Msgbox( 无法添加重复的项目)
Else
ListView1.Items.Add(payItem.ToString())。SubItems.AddRange({subpayItem.ToString(),subItem,unit,_
bridgeType,span,quantity.ToString(),Rate.ToString(),amount.ToString()})
结束 如果跨度>
HI guys,
I used this code to check if the item added exists in the listview.but it's not working.can you take a quick look?
Private Sub CheckDuplicates()
Dim itemI As ListViewItem
Dim itemJ As ListViewItem
Dim ok As Integer
For i As Integer = ListView1.Items.Count - 1 To 0 Step -1
itemI = ListView1.Items(i)
For j As Integer = i + 1 To ListView1.Items.Count - 1 Step 1
itemJ = ListView1.Items(j)
If itemI.SubItems(0).Text = itemJ.SubItems(0).Text AndAlso _
itemI.SubItems(1).Text = itemJ.SubItems(1).Text AndAlso _
itemI.SubItems(1).Text = itemJ.SubItems(2).Text AndAlso _
itemI.SubItems(1).Text = itemJ.SubItems(3).Text AndAlso _
itemI.SubItems(1).Text = itemJ.SubItems(4).Text AndAlso _
itemI.SubItems(1).Text = itemJ.SubItems(5).Text AndAlso _
itemI.SubItems(1).Text = itemJ.SubItems(6).Text AndAlso _
itemI.SubItems(1).Text = itemJ.SubItems(7).Text AndAlso _
itemI.SubItems(2).Text = itemJ.SubItems(8).Text Then
'Duplicate item found.
ok = MsgBox("A duplicate item is added,please remove the new item", vbCritical + vbOKCancel, _
"Bridge Construction Cost Estimate")
If ok = vbOK Then
ListView1.Items.Remove(itemJ)
End If
Exit For
End If
Next j
Next i
End Sub
解决方案
There are several issues:
1. The SubItem index for itemI was not incremented. See below
If itemI.SubItems(0).Text = itemJ.SubItems(0).Text AndAlso _ itemI.SubItems(1).Text = itemJ.SubItems(1).Text AndAlso _ itemI.SubItems(2).Text = itemJ.SubItems(2).Text AndAlso _ itemI.SubItems(3).Text = itemJ.SubItems(3).Text AndAlso _ itemI.SubItems(4).Text = itemJ.SubItems(4).Text AndAlso _ itemI.SubItems(5).Text = itemJ.SubItems(5).Text AndAlso _ itemI.SubItems(6).Text = itemJ.SubItems(6).Text AndAlso _ itemI.SubItems(7).Text = itemJ.SubItems(7).Text AndAlso _ itemI.SubItems(8).Text = itemJ.SubItems(8).Text Then 'Duplicate item found.
2. When youRemove
an item, the index number of all of the items with higher index numbers is decremented by one.
3. I am not sure about the secondFor
statement. I would use this:For j As Integer = ListView1.Items.Count - 1 to 0 Step -1
4. Need to add a check to ensure you are not comparing the same entry to itself.
If i<>j then If itemI.SubItems(0).Text = itemJ.SubItems(0).Text AndAlso _ itemI.SubItems(1).Text = itemJ.SubItems(1).Text AndAlso _ itemI.SubItems(2).Text = itemJ.SubItems(2).Text AndAlso _ itemI.SubItems(3).Text = itemJ.SubItems(3).Text AndAlso _ itemI.SubItems(4).Text = itemJ.SubItems(4).Text AndAlso _ itemI.SubItems(5).Text = itemJ.SubItems(5).Text AndAlso _ itemI.SubItems(6).Text = itemJ.SubItems(6).Text AndAlso _ itemI.SubItems(7).Text = itemJ.SubItems(7).Text AndAlso _ itemI.SubItems(8).Text = itemJ.SubItems(8).Text Then 'Duplicate item found ok = MsgBox("A duplicate item is added,please remove the new item", vbCritical + vbOKCancel, _ "Bridge Construction Cost Estimate") If ok = vbOK Then ListView1.Items.Remove(itemJ) End If End If
Check for duplicate before adding an item to the ListView control.
Dim itemJ As ListViewItem Dim bFound As Boolean = False For j As Integer = 0 To ListView1.Items.Count - 1 itemJ = ListView1.Items(j) If payItem.ToString() = itemJ.SubItems(0).Text AndAlso _ subpayItem.ToString() = itemJ.SubItems(1).Text AndAlso _ subItem = itemJ.SubItems(2).Text AndAlso _ unit = itemJ.SubItems(3).Text AndAlso _ bridgeType = itemJ.SubItems(4).Text AndAlso _ span = itemJ.SubItems(5).Text AndAlso _ quantity.ToString() = itemJ.SubItems(6).Text AndAlso _ Rate.ToString() = itemJ.SubItems(7).Text AndAlso _ amount.ToString = itemJ.SubItems(8).Text Then 'Duplicate item found. bFound = True Exit For End If Next If bFound Then Msgbox("Cannot add a duplicate item") Else ListView1.Items.Add(payItem.ToString()).SubItems.AddRange({subpayItem.ToString(), subItem, unit, _ bridgeType, span, quantity.ToString(), Rate.ToString(), amount.ToString()}) End If
这篇关于如何控制列表视图中的重复数据条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文