日期:2014-05-20  浏览次数:20580 次

COMBOBOX控件问题 麻烦大家了
如何实现在COMBOBOX控件中点击下拉箭头,在下拉列表里显示数据表TABLE1中一个字段   ZD的所有值。

------解决方案--------------------
数据绑定啊..

for example:

combobox绑定student数据库studentDetails表的sname字段..

SqlConnection con=new SqlConnection( "server=.;database=student;uid=sa;pwd=0421 ");
SqlDataAdapter sda=new SqlDataAdapter( "select * from studentDetails ",con);
DataSet ds=new DataSet();
sda.Fill(ds, "student ");
this.ComboBox1.DataSource=ds.Tables[ "student "];
this.ComboBox1.DisplayMember= "sname ";
------解决方案--------------------
第一种方法,用绑定的方法:
comboBox1.DataSource = 你的dataTable;
comboBox1.DisplayMember = "LongName ";
comboBox1.ValueMember = "ShortName " ;

第二种方法,用添加元素的方法:
DataTable dt = new DataTable();
//dt = getData();
ComboBox cb = new ComboBox();
for (int i = 0; i < dt.Rows.Count; i++)
{
cb.Items.Add(dt.Rows[i][ "指定列 "]);
}