首先定义一个公悔枣共的方法
public void BindComBox(ComboBox comBox,DataSet ds)
{
comBox.DataSource=ds.Tables[0].DefaultView;
comBox.DisplayMember = "Type";
comBox.ValueMember = "Type";
}
然后在页面上逗前游调山销用
Page_Load()
{
Maticsoft.BLL.T_Banci banci = new Maticsoft.BLL.T_Banci();
DataSet ds = banci.GetList("");
BindComBox(comBox1,ds);
BindComBox(comBox2,ds);
BindComBox(comBox3,ds);
BindComBox(comBox4,ds);
BindComBox(comBox5,ds);
BindComBox(comBox6,ds);..................................
}
两种写法,一:每个都要写,例如:
comboBox1.DataSource=ds.Tables[0].DefaultView;
comboBox1.DisplayMember = "Type";
comboBox1.ValueMember = "Type";
comboBox2.DataSource=ds.Tables[0].DefaultView;
comboBox2.DisplayMember = "Type";
comboBox2.ValueMember = "态链Type";
comboBox3.DataSource=ds.Tables[0].DefaultView;
comboBox3.DisplayMember = "Type";
comboBox3.ValueMember = "Type";
第二种方法是,循环窗体的控件,如果控件类型是combobox,就绑定,例如:
foreach(Control com in this.Controls)
{
if(com.controlType.toString()=="System.windows.form.ComboBox"){
com.DataSource=ds.Tables[0].DefaultView;
com.DisplayMember = "Type";
com.ValueMember = "Type";
}
}
以上代帆让孙码只是我手写的滑首,你看着写吧
直接遍历整个 控件集,在控件逗贺集中找到 ComboBox的实运李例
然后给每个ComboBox绑定上相同旁指迟的数据源。
foreach (Control control in Controls) {
if (control is ComboBox) {
ComboBox comboBox = (ComboBox)control;
comboBox.DataSource = ds.Tables[0].DefaultView;
//其他操作
}
}
comboBox1.DataSource=ds.Tables[0].DefaultView;
comboBox1.DisplayMember = "枣模Type";
comboBox1.ValueMember = "Type";
comboBox2.DataSource=ds.Tables[0].DefaultView;
comboBox2.DisplayMember = "Type";
comboBox2.ValueMember = "裤岩斗Type";
comboBox3.DataSource=ds.Tables[0].DefaultView;
comboBox3.DisplayMember = "Type";
comboBox3.ValueMember = "Type";
comboBox4.DataSource=ds.Tables[0].DefaultView;
comboBox4.DisplayMember = "Type";
comboBox4.ValueMember = "Type";
comboBox5.DataSource=ds.Tables[0].DefaultView;
comboBox5.DisplayMember = "Type";
comboBox5.ValueMember = "Type";
comboBox6.DataSource=ds.Tables[0].DefaultView;
comboBox6.DisplayMember = "Type";
comboBox6.ValueMember = "Type";
comboBox7.DataSource=ds.Tables[0].DefaultView;
comboBox7.DisplayMember = "Type"胡磨;
comboBox7.ValueMember = "Type";