Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
Hi,
How to set a default value in combobox.
 
Regards
Balamurugan
Posted 17-Jan-13 2:49am
Comments
Jim Jos at 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 at 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 at 18-Jan-13 2:34am
   
pls ask clearly....u want set any default value in combobox box right???
Balamurugan1989 at 18-Jan-13 3:10am
   
yes during run time values should not view afte we selecting the combobox only the values should display.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

To set the default item that is selected, just use (for example):
        
myComboBox.SelectedIndex = 5;        // set the 6th item in list as selected
  Permalink  
v2
Comments
muhammed riyaaz at 29-May-13 6:08am
   
thanx....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

 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;
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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()
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Peter Leow 313
1 Kornfeld Eliyahu Peter 220
2 Maciej Los 160
3 Sergey Alexandrovich Kryukov 157
4 OriginalGriff 153
0 Peter Leow 443
1 OriginalGriff 403
2 Kornfeld Eliyahu Peter 260
3 Sergey Alexandrovich Kryukov 242
4 Maciej Los 230


Advertise | Privacy | Mobile
Web04 | 2.8.150301.1 | Last Updated 21 Nov 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100