Results 1 to 5 of 5

Thread: Check if item exists before adding it

  1. #1
    ud2008 is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Jul 2010
    Posts
    148
    Reputation
    40

    Check if item exists before adding it

    I have a datagridview and I can add a selected item from the datagridview to a listview by double click the grid.
    This does work, but I want to check if the item I try to add does already exists in the listview.

    I have the following code, but this doesn't work:
    Code:
    Private Sub DataGridViewX1_DoubleClick(sender As System.Object, e As System.EventArgs) Handles DataGridViewX1.DoubleClick
            Dim lvItem As ListViewItem = ListView1.FindItemWithText(DataGridViewX1.SelectedCells(1).Value.ToString, True, 0)
            If (lvItem IsNot Nothing) Then
                Dim lvi As New ListViewItem
                lvi.Text = DataGridViewX1.SelectedCells(1).Value.ToString
                lvi.SubItems.Add(DataGridViewX1.SelectedCells(6).Value.ToString)
                ListView1.Items.Add(lvi)
    
                If ListView1.Items.Count > 0 Then
                    Timer1.Start()
                    ListView1.Enabled = True
                    PrintSonglistButton.Enabled = True
                    ClearAllButton.Enabled = True
                    ClearSelectedButton.Enabled = True
                    linesModified = True
                End If
            Else
                MessageBox.Show("Title already exists in the songlist!")
            End If
        End Sub
    Please help.

  2. #2
    Dunfiddlin's Avatar
    Dunfiddlin is offline VB.NET Forum Master
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2012
    Posts
    253
    Reputation
    31
    No it doesn't work and I'm totally baffled as to why you think it would. Timer? Try

    If ListView1.Items.Contains(lvi) Then etc.

    ... preferably before you add the item.

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,202
    Reputation
    2369
    Quote Originally Posted by ud2008
    Dim lvItem As ListViewItem = ListView1.FindItemWithText(DataGridViewX1.Selected Cells(1).Value.ToString, True, 0)
    If (lvItem IsNot Nothing) Then
    The return value is Nothing if an item is not found, your code would only add a new item if an item is found (return value ISNOT Nothing).
    Also, are you sure subitems should be included in the search, as your current code does?
    Quote Originally Posted by Dunfiddlin
    If ListView1.Items.Contains(lvi) Then etc.
    OP is trying to see if an item with specified text exist, not to check object reference for an existing ListViewItem.

  4. #4
    Dunfiddlin's Avatar
    Dunfiddlin is offline VB.NET Forum Master
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2012
    Posts
    253
    Reputation
    31
    Quote Originally Posted by JohnH View Post

    OP is trying to see if an item with specified text exist, not to check object reference for an existing ListViewItem.
    That may be what the code is doing but I'm not sure that it's what's intended ...

    I want to check if the item I try to add does already exists in the listview

  5. #5
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,202
    Reputation
    2369
    Dunfiddlin, please, you're just being argumentative. The "item" OP is checking for is a string....

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking