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

3

我试图将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 ???
}

请问您有什么想法吗?

1个回答

3

您可以通过以下方式保存所选值:

var Value1 = comboBox1.SelectedValue;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接