简体   繁体   中英

How to select an bounded combo box value and display its value into an Label Text

I have an ComboBox1 which is bounded by EmpId which is an primary key also.. when i used the code

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
      label5.Text = comboBox1.DisplayMember;
}

it gives an error-

Column 'EmpId' is constrained to be unique. Value 'Emp008' is already present.

My question is How to select that bounded value and display it into an Label text..

您可以使用此代码comboBox1.SelectedValue获取所选值,或者对于文本,可以使用此代码comboBox1.SelectedText

 private void bind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AttendanceManagmentSystem.Properties.Settings.Cons1"].ConnectionString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("Select EmpId from EmpDetail", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        comboBox1.DisplayMember = "EmpId";
        comboBox1.ValueMember = "EmpId";
        comboBox1.DataSource = dt;
        con.Close();
    }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        label5.Text = comboBox1.Text;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM