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

时间:2014-01-22 08:25:33

标签: c# combobox

我尝试将值从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 个答案:

答案 0 :(得分:3)

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

var Value1 = comboBox1.SelectedValue;