Click here to Skip to main content
15,891,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i insert new row into index zero of a databinded combo box, here is my code, but it doesn't seem to work.

C#
using ( SqlCommand command = new SqlCommand( "Select firstname from base_user_details", strCon ) )
                    {
                        command.CommandType = CommandType.Text;
                        //command.Parameters.Add( "@SelectOption", SqlDbType.VarChar, 25 ).Value = "staffFirstname";
                        //command.Parameters.Add( "@firstname", SqlDbType.VarChar, 50 ).Value = txtSearch.Text.Trim( );
                        using ( SqlDataAdapter adapter = new SqlDataAdapter( command ) )
                        {
                            using ( dt = new DataSet( ) )
                            {
                                adapter.Fill( dt );
                                cboSchools.DataSource = dt.Tables[0];
                                cboSchools.ValueMember="firstname";
                                cboSchools.DisplayMember ="firstname";
                                
                                cboSchools.Items.Insert( 0, "  ---Select---" );
                            }
                        }
                    }
Posted

first create one class like

C#
public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}


add code to use this class where you want to add item

C#
ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.Value = 12;
comboBox1.Items.Insert(0,item);
 
Share this answer
 
Comments
Uwakpeter 8-May-14 5:08am    
Please what is the value of 12 for?
Laiju k 8-May-14 5:57am    
combo box contains two values

text usually for displaying
value for saving into database

Try like this :
C#
using ( dt = new DataSet( ) )
                            {
                                adapter.Fill( dt );
                                cboSchools.DataSource = dt.Tables[0];
                                cboSchools.ValueMember="firstname";
                                cboSchools.DisplayMember ="firstname";
                                
                                cboSchools.Items.Add("---Select---",0 );
                            }
 
Share this answer
 
v2
Comments
Uwakpeter 8-May-14 5:07am    
Items.Add accepts only one argument
syed shanu 8-May-14 5:23am    
Yes try like this :
cboSchools.Items.Add("---Select---");
cboSchools.Items.Add(new Item("--Select--", 0));
do as below, before set the data source add row to datatable
C#
adapter.Fill(dt);
DataRow row = dt.Tables[0].NewRow();
row.ItemArray = new object[] { "--select--");
dt.Tables[0].Rows.InsertAt(row, 0);
cboSchools.DataSource = dt.Tables[0];
cboSchools.ValueMember="firstname";
cboSchools.DisplayMember ="firstname";
 
Share this answer
 
v2
Comments
Uwakpeter 8-May-14 5:12am    
its reporting error on dt.NewRow() and dt.Rows, there is no intellisence
DamithSL 8-May-14 5:34am    
oh, sorry, check my update
Uwakpeter 8-May-14 6:01am    
it works, thanks.

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