如何将组合框的第一个值设置为“选择选项" [英] How to set first value of combo box to 'Select Option'
本文介绍了如何将组合框的第一个值设置为“选择选项"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码填充组合框,但我想在组合框的第一个索引处输入选择选项"值.
I am using the following code to populate a combobox, but i want ''Select Option'' Value at first index of combobox.
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;
感谢您的帮助.
Usama
I shall be thankful for your help.
Usama
推荐答案
尝试一下:
Try this:
if(cmbCompName.Items.Count > 0)
{
//You can set using SelectedIndex
cmbCompName.SelectedIndex= 0;
}
使用..
Use..
cmbCompName.Insert(0,"Select Option");
或,
Or,
cmbCompName.Items.Insert(0, new ListItem("-Select Option-", "0"));
或,
在表单加载事件中,写类似..
Or,
In form load event write something like..
cmbCompName.Text = "Select Option";
然后在comboBox的 TextChanged 事件中编写以下代码..
And in the TextChanged event of the comboBox write the following code..
private void cmbCompName_TextChanged(object sender, EventArgs e)
{
if (cmbCompName.SelectedIndex < 0)
{
cmbCompName.Text = "Select Option";
}
else
{
cmbCompName.Text = cmbCompName.SelectedText;
}
}
您只需要下面突出显示的三行.大功告成:omg:
You needed just the below high lighted three lines. You are done. :omg:
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:
这篇关于如何将组合框的第一个值设置为“选择选项"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文