检查列表视图中的现有数据

Check existing Data in Listview

列表视图:lvData

    acctcode     first      last

1    DFNTN       High       Definition
2    BLNCE       World      Balance 
3    RNBOW       Rain       Bow

我想使用 textboxlvData 中添加数据。但首先程序必须检查 textbox 中提供的数据是否已经存在。基于 acctcode 列。

这是我的代码:

 Dim articlecheck As String = TEXTBOX.text
        Dim founditem As ListViewItem = LVDATA.FindItemWithText(articlecheck)
        If Not (founditem Is Nothing) Then
            MessageBox.Show("Data already exists!", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        Else
          'ADD DATA in the LISTVIEW'

问题是如果我将值放入文本框中,如下所示:DFNBLNRNB。我的程序仍然会显示 "Data alrleady exist!"

您必须遍历子项,因为 FindItemWithText 将在所有子项中搜索文本:

有代码:

Private Function FindSubItem(ByVal lv As ListView, ByVal SearchString As String) As Boolean
    'find column index in listview by name "acctcode"
    Dim idx = (From c In ListView1.Columns Where c.Text = "acctcode" Select c = c.Index).First()
    For Each itm As ListViewItem In lv.Items
        'search only subitems of column "acctcode"
        If itm.SubItems(idx).Text = SearchString Then Return True
    Next
    Return False
End Function

然后使用:

Dim articlecheck As String = TEXTBOX.text
    If FindSubItem(LVDATA, articlecheck) = True Then
      MessageBox.Show("Data already exists!", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Error)
      Exit Sub
    Else
      'add Your item
    End If

使用这些参数尝试相同的 FindItemWithText 方法。


 LVDATA.FindItemWithText(articlecheck,False,0,False)