adamchicago
asked on
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!
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
if the datagridview has more than 1 column, would u like to check for duplication in every column or only in a single column?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Data GridViewTe xtBoxColum n'. "
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
"Value of type 'String' cannot be converted to 'System.Windows.Forms.Data
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
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?
in your case the function should be modified to insert the values to the right columns.
can u post the columns for each control?
ASKER
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.
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.
@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?
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?
ASKER
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.
let me check this code for u...
ASKER
Thanks for your help