• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1163
  • Last Modified:

VB.Net - When moving items from listview to datagridview don't allow duplicates

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.  The items need to stay in listview, they cannot be removed to accomplish this.

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
  • 5
  • 4
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
if the datagridview has more than 1 column, would u like to check for duplication in every column or only in a single column?
0
 
Meir RivkinFull stack Software EngineerCommented:
check this code:
For Each item As ListViewItem In listView1.SelectedItems
    Dim listViewValues As Object() = item.SubItems.Cast(Of ListViewItem.ListViewSubItem)().[Select](Function(n) DirectCast(n.Text, Object)).ToArray()
    
    Dim duplicate As Boolean = False
    For Each row As DataGridViewRow In dataGridView1.Rows
        Dim gridRowValues As Object() = row.Cells.Cast(Of DataGridViewCell)().[Select](Function(n) n.Value).ToArray()
        Dim diff As IEnumerable(Of Object) = listViewValues.Except(gridRowValues)
        
        If diff.Count() = 0 Then
            duplicate = True
        End If
    Next
    
    If Not duplicate Then
        dataGridView1.Rows.Add(listViewValues)
    End If
Next

Open in new window

0
 
adamchicagoAuthor Commented:
sedqwick,  thanks again very much...the code above worked until I added 2 additional columns to the datagridview and then I got the error below at runtime: (can you help me get past this?)

"Value of type 'String' cannot be converted to 'System.Windows.Forms.DataGridViewTextBoxColumn'.      "

When the user button clicks it adds the items from the listview to the datagridview (via your code above), but there need to be two additional columns in the datagridview that are for user input and do not need to be considered for duplication...2nd column- user keys in values, 3rd column is a combobox that user selects
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
Meir RivkinFull stack Software EngineerCommented:
i've posted in the first thread u created that this code counts that both listview and datagrid has the same number of columns (and ultimately same column names).
in your case the function should be modified to insert the values to the right columns.

can u post the columns for each control?
0
 
adamchicagoAuthor Commented:
Sorry...I saw that in your 1st post but I incorrectly assumed that since the additional columns in the datagridview were going to be populated manually from the user that it wouldn't matter...oops.

Controls/Columns:
Listview1: has 1 column named:
-Column1 Name: "Measures" ...this is the source for the datagridview "type" column

Datagriview1: has 3 columns named:
-Column1 Name: "type" ...this is the only column that items from Listview1 are moved into via button click
-Column2 Name: "name"  ...this will be a column that the user can key-in text
-Column3 Name: "dependant"  ...this column will be a combobox that the user can select from (source will ultimately be from the "name" column.
0
 
Meir RivkinFull stack Software EngineerCommented:
@adamchicago

so when user click button, u wish to check for duplicate only in the 'type' column of the datagridview, right?
if true (duplicate), do not add row from listview, otherwise, add new row to datagridview but leave 'name' and 'dependant' columns blank?
0
 
adamchicagoAuthor Commented:
sedqwick, if I understood you right yes.  Duplicates are only checked on the "type" column and the others are for user input but are not checked for dups...thanks again.
0
 
Meir RivkinFull stack Software EngineerCommented:
let me check this code for u...
0
 
adamchicagoAuthor Commented:
Thanks for your help
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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