加载数据从SQL到组合并保存所选的值

我尝试将值从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;
上一篇:CodeGo.net>使ComboBox只接受特定类型


下一篇:java-如何使用SmartGWT手动/以编程方式打开ComboboxItem / SelectItem的pickList?