0

当从数据库中获取数据时根据名称填写到组合框...</p>

SqlConnection con = new SqlConnection(@"Data Source=CENTAUR09-PC\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=true");
SqlCommand cmd = new SqlCommand("Select cust_name from customer", con);
con.Open();
comboBox1.DataSource = cmd;
comboBox1.DisplayMember = cmd.ToString();
con.Close();

... 数据源和显示成员是什么?

4

1 回答 1

3

像这样的东西:

DataTable dt = new DataTable();

    using(SqlConnection con = new SqlConnection(@"Data Source=CENTAUR09-PC\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=true"))
    {
       SqlCommand cmd = new SqlCommand("Select id, cust_name from customer", con); 

       SqlDataAdapter adapter = new SqlDataAdapter(cmd);
       adapter.Fill(dt);

       comboBox1.DataSource = dt.DefaultView; 
       comboBox1.DisplayMember = "cust_name"; 
       comboBox1.ValueMember = "id"; 
    }

编辑:

最好选择客户的 PK 并将其路径设置为 VlaueMember

于 2012-11-27T09:47:37.857 回答