Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void BindCat()
{
        ddlCategory.DataSource=objProdCat.SelectAll();
        ddlCategory.DisplayMember = "ProductCatName";
        ddlCategory.ValueMember = "ProductCatId";
        ddlCategory.Items.Insert(0, "Add new Category"); 
        //MessageBox.Show(ddlCategory.SelectedValue.ToString()); 
}

I want to add a custom option to a already bound combobx but this is not working. It is now showing me values 1,2,3,4,5

How to add a custom field to a already bound combobox?
Posted

1 solution

You could either add the default text to the Text property of the combobox like this (preferred):

ddlCategory.Text = "Add new Category";

Or, you could add the value to the datatable directly:

if you are using datatable
da.Fill(dt);
DataRow row = dt.NewRow();
row["Category"] = "Add new Category";
dt.Rows.InsertAt(row, 0);
ddlCategory.DataSource = dt;
}


if you are using list of class
objProdCat=getinglist
objProdCat.Insert(0,"Add new Category");
ddlCategory.DataSource=objProdCat.SelectAll(); 
 
Share this answer
 
v2
Comments
Muhamad Faizan Khan 26-Apr-14 6:42am    
da.Fill(dt);
DataRow row = dt.NewRow();
row["Category"] = "Add new Category";
dt.Rows.InsertAt(row, 0);
ddlCategory.DataSource = dt;
}
this is not showing any thing in combobx
[no name] 26-Apr-14 6:56am    
what do you mean?<br>
Have you checked there "Add new category" is available in 0 index of dt, or not?

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