怎么把数据库里的一列添加到combobox中?
那位大神可以帮忙啊?很急的。我想把作家那张表里的作家姓名添加到combobox中,调试的时候我把combobox控件点开里面是空的,我应该怎么写代码?越详细越好。 谢谢!
假设combobox绑定的列表为DataSet2的ListTable表(含有ListID, ListName字段),需要绑定的记录字段为DataSet1的Table1表的ListID字段
combobox.DataSource = dataset2.Tables["ListTable"];
combobox.DisplayMember = "ListName";
combobox.ValueMember = "ListID";
combobox.DataBindings.Add("SelectedValue", dataset1, "Table1.ListID");
2.*****************************************************************
//dt为数据表,ID,Name为dt的两个字段:
comboBox1.DataSource = dt ;
comboBox1.ValueMember ="ID";
comboBox1.DisplayMember ="Name";
3.******************************************************************
SqlConnection con = new SqlConnection("server=192.168.2.198;uid=sa;pwd=sa;database=northwind");
SqlCommand cmd = con.CreateCommand();
= "Select * from Customers where country='USA'";
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds, "Customers");
comboBox1.DataSource = ds.Tables["Customers"];
comboBox1.DisplayMember = "CompanyName";
comboBox1.ValueMember = "CompanyName";