private void Form1_Load_1(object sender, EventArgs e)
{
string Sql = "select status from lk_tb_project_status";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(Sql, con);
con.Open();
OleDbDataReader DR = com.ExecuteReader();
while (DR.Read())
{
comboBox1.Items.Add(DR[0]);
}
}我从数据库中获取值并在组合框(下拉列表)中显示。数据库中有两个被出售和打开的值。我想要的是在页面加载时更改选定的项。有人能帮我吗?
发布于 2014-02-26 11:44:47
使用FindString方法获取ComboBox中带有文本"Sold“的第一个项目的索引。然后将SelectedIndex设置为:
comboBox1.SelectedIndex = comboBox1.FindString("Sold");如果它找不到文本"Sold“,那么所选的索引将是-1,并且组合框将默认显示一个空白项。
发布于 2014-02-26 11:43:58
如果要将第一项设置为选择项,请执行以下操作。
试试这个:
comboBox1.SelectedItem = comboBox1.Items[0]; 或
comboBox1.SelectedIndex = 0; //it is the index value发布于 2014-02-26 11:46:24
string Sql = "select status from lk_tb_project_status";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(Sql, con);
con.Open();
comboBox1.Items.Insert(0,"Select Status");
OleDbDataReader DR = com.ExecuteReader();
while (DR.Read())
{
comboBox1.Items.Add(DR[0]);
}https://stackoverflow.com/questions/22040152
复制相似问题