Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 combo box in my application. i am display data in combo box in page_load event from database.i want display "-Select-" by default value in combo box. and after that display database value in combo box.
Posted

Use This
C#
SqlCommand comm = new SqlCommand("select * from EmpDetail");
            SqlDataAdapter adap = new SqlDataAdapter(comm);
            comm.Connection = conn;
            DataSet ds = new DataSet();
            adap.Fill(ds);
            comboBox1.Text = "-select-";
           foreach(DataRow dr1 in ds.Tables[0].Rows)
           {
               comboBox1.Items.Add(dr1["EmpName"].ToString());
           }


Hope this will wok
 
Share this answer
 
v2
Comments
member60 11-Sep-12 7:58am    
my 5!
After Binding CombooBox.
C#
if(ComboBox1.Items.Count>0)
{
ComboBox1.Items.Insert(0, "Select Item");
ComboBox1.SelectedIndex = 0;
}
 
Share this answer
 
After binding the combobox from database you need to add one extra item at 0th Index as you required.
Try this:
C#
ComboBox1.Items.Insert(0, new ComboBoxItem("-Select-", string.Empty));


--Amit
 
Share this answer
 
Comments
Member 10892284 5-Jul-14 2:16am    
ComboBox1.Items.Insert(0, new ComboBoxItem("-Select-", string.Empty));

this code is not working,, giving red error in "ComboBoxItem"

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