Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to set a default value in combobox.

Regards
Balamurugan
Posted
Comments
Jim Jos 17-Jan-13 7:56am    
default value?? U mean the item selected when the combox gets focus? Use selecteditem or selectedindex properties.. Otherwise please explain what do you mean by default value?
Balamurugan1989 18-Jan-13 0:20am    
During Runtime the combo box values should not display.After clicking the combobox values should view.For me the values is getting fetched in Combobox.
Deenuji 18-Jan-13 2:34am    
pls ask clearly....u want set any default value in combobox box right???
Balamurugan1989 18-Jan-13 3:10am    
yes during run time values should not view afte we selecting the combobox only the values should display.

To set the default item that is selected, just use (for example):
C#
myComboBox.SelectedIndex = 5;        // set the 6th item in list as selected
 
Share this answer
 
v2
Comments
riodejenris14 29-May-13 6:08am    
thanx....
public void FillCmbo(String Sql, ComboBox cmb, String name, String id)
       {
           try
           {
               if (Con.State == ConnectionState.Open)
                   Con.Close();
               Con.Open();

               OleDbDataAdapter Ada = new OleDbDataAdapter(Sql, Con);
               DataTable Dte = new DataTable();
               Ada.Fill(Dte);
               DataRow Drw;
               Drw = Dte.NewRow();
               Drw.ItemArray = new object[] { 0, "<----Select---->" };
               Dte.Rows.InsertAt(Drw, 0);
               cmb.DisplayMember =name;
               cmb.ValueMember = id;
               cmb.DataSource = Dte;
           }
           catch (System.Exception ex)
           {

               MessageBox.Show(ex.Message, "Check", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
       }


/................you can also use
ComboBox.selectIndex=0;
 
Share this answer
 
v2
In the Designer (definition for InitializeComponent() in your form), just put
this.comboBox1.SelectedIndex = 0; // or whatever default value

before
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

just to ensure you don't accidentally trigger the index changed event.
 
Share this answer
 
If we are binding the database from table to combobox means it can't set a default value.But its not the right thing to do because it won't be secure.Writing Query is the best option for retriving datas from table.The below code is to get the datas from table to combobox,
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\xxx.acdb;Persist Security Info=False;");
con.Open();
OleDbCommand cmd = new OleDbCommand("SELECT Distinct aaa FROM bbb", con);
dr = cmd.ExecuteReader();
comboBox4.Items.Clear();
while (dr.Read())
{
comboBox4.Items.Add(dr[0].ToString());
}
dr.Close();
con.Close()
 
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