[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

VB.Net - Move items from Listview to DataGridView on Button Click (no dups)

Experts: Please help me convert the code below that currently transfers non-duplicate items on a button click from one listview to another to transferring from a listview to datagridview.

Details:
- Listview1 is an already populated listview that upon a button click transfers highlighted (non-duplicate) items to another listview (lvCodeElts)
- importcolumnsDGV is the name of a new datagridview that will replace the 2nd listview

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 LISTBOX ON LEFT TO IMPORT MEASURES ON RIGHT EXCEPT FOR DUPLICATES VIA BUTTON CLICK
        For i As Integer = 0 To ListView1.SelectedIndices.Count - 1
            Dim lvi As ListViewItem = ListView1.Items(ListView1.SelectedIndices(i)).Clone
            Dim match As ListViewItem = lvCodeElts.FindItemWithText(lvi.Text)
            If IsNothing(match) Then
                lvCodeElts.Items.Add(lvi)
            End If
        Next
    End Sub

Open in new window

0
adamchicago
Asked:
adamchicago
  • 3
  • 2
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
change the code to the snippet.
i took into account that both listview and datagridview has the same number of columns.
For Each item As ListViewItem In ListView1.SelectedItems
            DataGridView1.Rows.Add(item.SubItems.Cast(Of ListViewItem.ListViewSubItem)().[Select](Function(n) DirectCast(n.Text, Object)).ToArray())
        Next

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
basically, it takes the selected items in the listview, create an array of objects which reflects the text values of the subitems of each listview item.
and insert them into the datagridview as anew row
check the screenshot attached
Untitled.jpg
0
 
adamchicagoAuthor Commented:
sedqwick, awesome...thanks!

However duplicate items are transferring...how do you stop the dups?

In other words if there is a list of fruit in the Listview1 and you highlight and button click on "Apple", then "Apple" appears in the data grid (that's great!), however the code needs to not transfer a duplicate entry of "Apple" into the datagridview if one exists already (there should never be a duplicate item in the datagridview).

Thanks again for your help!
0
 
adamchicagoAuthor Commented:
Thanks for your help!
0
 
Meir RivkinFull stack Software EngineerCommented:
duplicate means that all sub items are identical or even if one column is identical?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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