我尝试将值从sql数据库加载到组合框,然后单击按钮后,我想将组合框的选定值保存到变量.
这是我的代码:
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
string strCmd = "select id,Prijmeni from Osoba";
SqlCommand cmd3 = new SqlCommand(strCmd, con);
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Prijmeni";
comboBox1.ValueMember = "id";
comboBox1.Enabled = true;
cmd3.ExecuteNonQuery();
con.Close();
private void Ulozit_Click(object sender, EventArgs e)
{
//How i could save selected value of combobox to variable Value1 ???
}
请问您有什么想法吗?
解决方法:
您可以通过以下方式保存所选值:
var Value1 = comboBox1.SelectedValue;