Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to know that how to add extra item in combo box with datasource?

My code is

C#
public Fill(List<string> contractor)
       {
          comboBox1.Sorted = true;
          contractor.Insert(0, "All");


           comboBox1.DataSource = contractor;



       }


After adding datasource, item("All") is shown in between.
But I want to show it on top of combo box.
Posted
Comments
Member 7909353 19-Nov-13 4:06am    
If I modifiy after fill combo box then a warning is shown
"Additional information: Items collection cannot be modified when the DataSource property is set."

Hello ,

if you are using DataTable to bind with combobox then try this code

DataRow dr = dt.NewRow(); //dt is DataTable
dr[0] = "Please Select";//Add the item in Datarow.Here  at 0 position. you can change the position according to your requirement
dt.Rows.InsertAt(dr, 0);//add the row in datatable at top position
cmbcategory.DataSource = dt;
cmbcategory.DisplayMember = "Category";
cmbcategory.ValueMember = "Id";


thanks
 
Share this answer
 
Comments
Member 7909353 19-Nov-13 3:52am    
I am using List<string> which is already filled then fill the combo box.
Animesh Datta 19-Nov-13 4:03am    
Hello ,what ever you have tried previously with List<string> is working for me perfectly .
my code is same as like your code .

List<string> contractor = new List<string>();
contractor.Add("Animesh");
contractor.Add("Kriss");
contractor.Add("Madhu");
contractor.Insert(0, "Select");
cmbcategory.DataSource = contractor;
Member 7909353 19-Nov-13 4:10am    
Ok!
contractor.Add("Abcd");
contractor.Add("ab");
contractor.Insert(0, "All");
cmbcategory.Sorted = true;
cmbcategory.DataSource = contractor;
It does not show item All on top of combo box
Animesh Datta 19-Nov-13 4:35am    
combobox sorts alphabetically.here 'All' comes after 'ab'
thats why 'All' comes later .if you stop the sorting then output will come easily .
Sorted combo box always sort the list alphabetically. So set the sort to false and then insert it works perfectly.

If you want to the list to be sorted please call List.Sort before insert and then assign to data source.

Hope these helps
 
Share this answer
 
Comments
Member 7909353 19-Nov-13 5:25am    
I want combo box to be sorted and item "All" at position zero.
try

Datatable Dt = new Datatable();
Dt.Columns.Add("Id");
Dt.Columns.Add("Name");
Dt.Rows.Add("0","Item 1");
Dt.Rows.Add("1","Item 2");
combobox.DataSource=Dt;
combobox.ValueMember="Id";
combobox.DisplayMember="Name";
 
Share this answer
 
It is done

C#
public Fill(List<string> contractor)
        {
contractor.Sort();
            comboBox1.Items.Insert(0, "All");
            for (int i = 0; i < contractor.Count; i++)
            {
                
                comboBox1.Items.Add(contractor[i]);
            }
           comboBox1.SelectedIndex = 0;
}</string>

but I want to know that it is better than DataSource property is set.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900