Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1273
  • Last Modified:

VB.Net Add NON-DUPLICATE items to DataGridView from Listview

Experts: this code moves highlighted items from a listview to a datagridview on a button click, however I need help converting the code so that duplicate items are not added to the datagridview.

For example if "Apple" already has been added to the datagridview from the listview, and the user tries to highlight Apple in the listview and button click to move it over to the datagridview the code should not allow it...so that the datagridview has only one occurrence of "Apple" in it.

Thank you very much for any help!
Private Sub addmsrBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addmsrBTN.Click
        
'COPY ITEMS FROM AVAILABLE MEASURES LISTVIEW ON LEFT TO IMPORT MEASURES DATAGRIDVIEW ON RIGHT EXCEPT FOR DUPLICATES VIA BUTTON CLICK
        
For Each item As ListViewItem In ListView1.SelectedItems
            importcolumnsDGV.Rows.Add(item.SubItems.Cast(Of ListViewItem.ListViewSubItem)().[Select](Function(n) DirectCast(n.Text, Object)).ToArray())
        Next

    End Sub

Open in new window

0
adamchicago
Asked:
adamchicago
1 Solution
 
jpaulinoCommented:
You don't need to change that code, just need to lock the selection in the Listview.
Something like this:

    Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
        If e.IsSelected Then

            ' Looks in the DataGridView for the item
            For Each row As DataGridViewRow In Me.DataGridView1.Rows
                If row.Cells(0).Value = e.Item.Text Then

                    ' Removes the selection
                    Me.ListView1.SelectedItems(e.ItemIndex).Selected = False

                End If
            Next

        End If
    End Sub

Open in new window

0
 
adamchicagoAuthor Commented:
Thanks for your help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now