Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
i want to add an empty string/blank to index[0] in combobox, the problem is that combobox is databound.I want to display items starting from index[1].
Posted
Updated 16-Dec-13 23:14pm
v2

Hi Try like this,
You cannot modify the Items , once it is databound.
option is , you can modify the data source as below.


C#
// Trying with List
            List<string> lst = new List<string>();
            lst.Add("one");
            lst.Add("two");
            lst.Add("three");
            lst.Insert(0, "");
            comboBox1.DataSource = lst;


            // Trying with DataTable
            DataTable dt = new DataTable ();
            dt.Columns.Add("Numbers", typeof(string));
            dt.Rows.Add("one");
            dt.Rows.Add("two");
            dt.Rows.Add("three");
            DataRow newrow = dt.NewRow();
            newrow["Numbers"] ="";
            dt.Rows.InsertAt(newrow, 0);
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "Numbers";</string></string>
 
Share this answer
 
Comments
uditCsharp 17-Dec-13 5:45am    
thank you sir..

i used datatable... We just need to add ...

comboBox1.Text="";
Karthik_Mahalingam 17-Dec-13 5:47am    
ok fine :)
C#
clsOrder oOrder = new clsOrder();
        IDataReader iDr = oOrder.GetReason();
        ddlReason.Items.Clear();
        ddlReason.SelectedValue = null;
        ddlReason.DataSource = iDr;
        ddlReason.DataTextField = "ReasonName";
        ddlReason.DataValueField = "ReasonId";
        ddlReason.DataBind();

        iDr.Close();
        iDr.Dispose();
        ddlReason.Items.Insert(0, new ListItem(" ", ""));

It will help you
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 17-Dec-13 6:25am    
HI Renuka
whenever you are posting an answer, add it inside the code block,
it will be easier for the user to understand the code in single look..
Renuka Ruke 17-Dec-13 6:47am    
ok and thanks
Karthik_Mahalingam 17-Dec-13 7:56am    
Good, welcome :)

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