Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Duplicate Items from Listbox

Status
Not open for further replies.

Ddraig

MIS
Jul 16, 2004
97
US
Howdy,

I am trying to figure out how to remove duplicate items from a list box.

This is the Code to load my list box.

Dim lbcount As Integer
Dim i As Integer
Dim lbSelected As String
If chkLBSerial.Checked = True Then
ListBox1.DataSource = DataSet1.Tables("tblPCInventory")
ListBox1.DisplayMember = "PCI_Serial"

Else
ListBox1.DataSource = DataSet1.Tables("tblPCInventory")
ListBox1.DisplayMember = "PCI_FC"
End If

And I have placed a button on the form to try and remove duplicates. I am able to get it to work with just a general collection of items, but this is loading a list from a dataset

Code for the Button is:

Dim cntr As Integer
For cntr = ListBox1.Items.Count - 1 To 1 Step -1
' If next item is a duplicate -> Remove It
If ListBox1.Items(cntr) = ListBox1.Items(cntr - 1) Then _
ListBox1.Items.RemoveAt(cntr)
Next

However I am getting the following Error Message.

Operator is not valid for type 'DataRowView' and type 'DataRowView'.


It is almost like it is trying to remove the whole row and not the item listed in the list box. I think I'll explain a bit to see if I can get some possible suggestions since this is the first time in a few years I've started programming again.

These are some of the Tables in my database, a PC_Name and a PC_History. Basically we have given all our PCs Id Names, and would like to be able to pull their history information. So I would like to populate a listbox with the names of the PCs, (easy part) removing the duplicates (where I'm stuck) and then display all of the history related to the selected PC. For Example: I click on PC-101 and it scrolls through the database collecting all of the history from PC-101 and then displays it.

I guess my concern is that I don't want it to actually delete the entire row, but just the duplicate in the listbox so I can use that as search criteria.

Thanks,
Dd.
 
I use CONTAINS to move things around i.e.


If ListBox2.Items.Contains(ListBox1.SelectedItem) = False Then
ListBox2.Items.Add(ListBox1.SelectedItem)
ListBox1.Items.Remove(ListBox1.SelectedItem)
ListBox2.Sorted = True
End If

 
I think the problem has to do with the binding. if you check the listbox1.Items(cntr).Type I'm guessing it'll come up as a data row. If you remove the data row, the dataset will try to update the dataset that you are bound to.

I've hated bound data ever since DAO. More headaches then it's worth. Just read the data in directly and drop the dataset (Cheap fix), or create a data abstraction object that seperates the GUI from the Data (Good fix).

-Rick

----------------------
 
That is what I was thinking too... I was considering loading the data into an array then manipulating the array to display the appropirate information. Or is their a better/easier way to do this? I'm a bit of a Noob. :)
 
It matters on how strict and intense you want to get. The project I'm currently working on splits everything into 5 categories. Database, DataObject, BuisnessObject, WorkFlow, GUI. This complete extraction of all of the elements means that everything we are working on is highly modular and we can (and do!) reuse it. But it's a bit excessive for many smaller projects.

If this is going to be a one time shot and isn't a huge project, I'd say yeah, just read the data into an array (You can even just use the list collection of the combo box). If you might reuse this more often, or if the overall data structure lends itself to the idea, you could build a data object to get/hold/update the data.

-Rick

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top