Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
I am using the following code to populate a combobox, but i want 'Select Option' Value at first index of combobox.
C#
string strConn = "Data Source=SEZSW08;Initial Catalog=Nidhi;Integrated Security=True";
        SqlConnection Con = new SqlConnection(strConn);
        Con.Open();
        string strCmd = "select companyName from companyinfo where CompanyName='" + cmbCompName.SelectedValue + "';";
        SqlCommand Cmd = new SqlCommand(strCmd, Con);
        SqlDataAdapter da = new SqlDataAdapter(strCmd, Con);
        DataSet ds = new DataSet();
        Con.Close();
        da.Fill(ds);
        cmbCompName.DataSource = ds;
        cmbCompName.DisplayMember = "CompanyName";
        cmbCompName.ValueMember = "CompanyName";
        //cmbCompName.DataBind();
        cmbCompName.Enabled = true;


I shall be thankful for your help.
Usama
Posted

Try this:
C#
if(cmbCompName.Items.Count > 0)
{
  //You can set using SelectedIndex
  cmbCompName.SelectedIndex= 0;
}
 
Share this answer
 
Comments
[no name] 28-Jul-13 17:05pm    
5!
Use..
C#
cmbCompName.Insert(0,"Select Option");

Or,
C#
cmbCompName.Items.Insert(0, new ListItem("-Select Option-", "0"));

Or
,
In form load event write something like..
C#
cmbCompName.Text = "Select Option";

And in the TextChanged event of the comboBox write the following code..
C#
private void cmbCompName_TextChanged(object sender, EventArgs e)
       {
           if (cmbCompName.SelectedIndex < 0)
           {
               cmbCompName.Text = "Select Option";
           }
           else
           {
               cmbCompName.Text = cmbCompName.SelectedText;
           }
       }
 
Share this answer
 
v3
Comments
osama.javed 28-Jul-13 8:51am    
I have done this
cmbCompName.Text = "Select Option";
private void cmbCompName_TextChanged(object sender, EventArgs e)
{
if (cmbCompName.SelectedIndex < 0)
{
cmbCompName.Text = "Select Option";
}
else
{
cmbCompName.Text = cmbCompName.SelectedText;
}
}
but on pageload, it does not shows the 'select option' value
ridoy 28-Jul-13 9:00am    
read carefully,in your page load event just write cmbCompName.Text = "Select Option"; and then in textchanged event of comobox write the rest of code.If still not then try other 2 alternative solution above.
Joezer BH 28-Jul-13 9:42am    
5ed
ridoy 28-Jul-13 9:55am    
Thanks Maimonides :)
You needed just the below high lighted three lines. You are done. :omg:
C#
string strConn = "Data Source=SEZSW08;Initial Catalog=Nidhi;Integrated Security=True";
        SqlConnection Con = new SqlConnection(strConn);
        Con.Open();
        string strCmd = "select companyName from companyinfo where CompanyName='" + cmbCompName.SelectedValue + "';";
        SqlCommand Cmd = new SqlCommand(strCmd, Con);
        SqlDataAdapter da = new SqlDataAdapter(strCmd, Con);
        DataTable ds = new DataTable();
        Con.Close();
        da.Fill(ds);
        DataRow dr = ds.NewRow();//Created new row 
        dr("companyName ") = "Select option";//Added required text
        ds.Rows.InsertAt(dr, 0);//Added at first position
        cmbCompName.DataSource = ds;       
        cmbCompName.DataBind();

Regards..:laugh:
 
Share this answer
 
v2
C#
cmbCompName.SelectedText = "your value";
or
cmbCompName.SelectedIndex = 0;
 
Share this answer
 
v3

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