Click here to Skip to main content
11,639,765 members (64,625 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# combobox
I have populated the combo box with data from a database and I wanted to remove the duplicate names from the combo box items list. Can you give me a code to do it?
Posted 30-Jun-12 22:18pm
wolfsor562
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It's not necessarily as simple as that - it depends to an extent on what type of items you have filled your ComboBox with. Because you can't just set the Items array (it exposes no public setter, just the getter) you have to create a new array of items, remove the duplicates, and set it back into the ComboBox:

            List<object> list = new List<object>();
            foreach (object o in myComboBox.Items)
                {
                if (!list.Contains(o))
                    {
                    list.Add(o);
                    }
                }
            myComboBox.Items.Clear();
            myComboBox.Items.AddRange(list.ToArray());
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Why not just fetch the unique ones from database itself?
Like:
SELECT DISTINCT Names FROM MstNames

If you still want to, before assigning the datasource, filter the datatable to be used.
To know more of it, read here: MSDN: DataView.RowFilter Property[^]
  Permalink  
Comments
Jayanta Chatterjee at 11-Mar-13 8:34am
   
My 5 for Best Solution...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

As Sandeep's answer best way is select distinct when you select from database.

if you are using c# 3.0 or later you can try below code

var itemArry= myComboBox.Items.SelectMany(i => i).Distinct().ToArray();
myComboBox.Items.Clear();
myComboBox.Items.AddRange(itemArry);
  Permalink  
Comments
Jayanta Chatterjee at 11-Mar-13 8:35am
   
My 4 for second Best Solution..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

syntax :
if(!cmbPutDB.Items.Contains(your_entry_name)
                        cmbPutDB.Items.Add(your_entry_name);
ex :
 // To get all database from the server and list with combobox cmbPutDB 
SqlConnection sqlCon = new SqlConnection("Server='sw-projects';uid ='sa'; pwd = 'sa'");
sqlCon.Open();
SqlCommand sqlCmnd = new SqlCommand("Select * from sys.Databases", sqlCon);
IDataReader dataReader = sqlCmnd.ExecuteReader();
while (dataReader.Read())
{
    if(!cmbPutDB.Items.Contains(dataReader["Name"]))
    cmbPutDB.Items.Add(dataReader["Name"]);
}
sqlCon.Close();
  Permalink  
v2
Comments
CHill60 at 15-Jul-15 7:39am
   
Question is 2 years old and already adequately answered. This code does not answer the question asked and is pretty poorly written.
Wes Aday at 15-Jul-15 7:49am
   
Yeah, he's a rep hunter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Suvendu Shekhar Giri 330
1 CPallini 177
2 F-ES Sitecore 170
3 himanshu agarwal 167
4 Sergey Alexandrovich Kryukov 159
0 OriginalGriff 9,081
1 Sergey Alexandrovich Kryukov 8,847
2 Mika Wendelius 7,027
3 Suvendu Shekhar Giri 2,650
4 F-ES Sitecore 2,558


Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 15 Jul 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100