如何从数据库中将数据加载到组合框中

kar*_*ddy 3 c# combobox winforms

如何从数据库中将数据加载到组合框中?我想在表单中的组合框中显示supportID.我正在使用的代码粘贴在这里.我在formload中调用BindData().我得到例外:无法绑定到新的显示成员.参数名称:newDisplayMember.我用的代码是:

public void BindData()
    {
        SqlConnection con = new SqlConnection(@"server=RSTT2; database = Project ;  User Id=sa; Password=PeaTeaCee5#");
        con.Open();
        string strCmd = "select supportID from Support";
        SqlCommand cmd = new SqlCommand(strCmd, con);
        SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cbSupportID.DataSource = ds;
        cbSupportID.DisplayMember = "supportID";
        cbSupportID.ValueMember = "supportID";
        cbSupportID.Enabled = true;
        cmd.ExecuteNonQuery();
        con.Close();

    }
Run Code Online (Sandbox Code Playgroud)

Kin*_*ing 7

DataSourcecombobox应该是DataTable在这种情况下,试试这个:

cbSupportID.DataSource = ds.Tables[0];
Run Code Online (Sandbox Code Playgroud)

或者更好的是,您应该将数据填充到a DataTable而不是DataSet像这样:

DataTable dt = new DataTable();
da.Fill(dt);
//...
cbSupportID.DataSource = dt;
Run Code Online (Sandbox Code Playgroud)