SqlCommand MyCommand1 = new SqlCommand("select * from produce_cn", myconnection);

SqlDataReader dr = MyCommand1.ExecuteReader();
CheckBoxList1.DataSource = dr;
CheckBoxList1.DataTextField ="produce_name";
CheckBoxList1.DataValueField ="id";
CheckBoxList1.DataBind(); CheckBoxList2.DataSource = dr;
CheckBoxList2.DataTextField ="produce_name";
CheckBoxList1.DataValueField ="id";
CheckBoxList2.DataBind(); CheckBoxList3.DataSource = dr;
CheckBoxList3.DataTextField ="produce_name";
CheckBoxList1.DataValueField ="id";
CheckBoxList3.DataBind(); CheckBoxList4.DataSource = dr;
CheckBoxList4.DataTextField ="produce_name";
CheckBoxList1.DataValueField ="id";
CheckBoxList4.DataBind(); CheckBoxList5.DataSource = dr;
CheckBoxList5.DataTextField ="produce_name";
CheckBoxList1.DataValueField ="id";
CheckBoxList5.DataBind();
我的表结构 如下
id  produce_name produce_type
produce_type 从 1-5
能一次取出所有的数据。(当然如果建立5次连接,用不个不同的sql 来作肯定可以 那就太低效了)
然后 produce_type  =1 的绑定道 checkboxlist1 上, produce_type  =2 的绑定道 checkboxlist2 上.用produce_name  做checkboxlist 的text, id 做chekboxlist的value代码肯定有问题 应该怎么绑定呢?

解决方案 »

  1.   

    建一个ds。把1-5的所有数据读取出来。再通过dv视图来进行查询操作。
      

  2.   

    同样的sql但是用SqlDataAdapter.Fill(dst);//dst是dataset
    然后定义一个DataView重复使用.
    DataView dv=dst.Tables[0].DefaultView;
    dv.RowFileter="produce_type  =1";
    //绑定一个列表
    DataView dv=dst.Tables[0].DefaultView;
    dv.RowFileter="produce_type  =2";
    //绑定第二个列表
      

  3.   

    select * from produce_cn 
    use SqlDataAdapter to fill a DataTable, sayDataTable dt = new DataTable();
    da.Fill(dt);DataView dv = new DataView(dt);
    dv.RowFilter = "produce_type  =1";CheckBoxList1.DataSource = dv;
    CheckBoxList1.DataTextField ="produce_name";
    CheckBoxList1.DataValueField ="id";
    CheckBoxList1.DataBind();dv.RowFilter = "produce_type  =2";CheckBoxList2.DataSource = dv;
    CheckBoxList2.DataTextField ="produce_name";
    CheckBoxList2.DataValueField ="id";
    CheckBoxList2.DataBind();//or
    for (int i=1; i <=5; i++)
    {
      dv.RowFilter = "produce_type  = " + i.ToString();
      CheckBoxList cb= (CheckBoxList)FindControl("CheckBoxList" + i.ToString());
      cb.DataSource = dv;
    cb.DataTextField ="produce_name";
    cb.DataValueField ="id";
    cb.DataBind();}
      

  4.   

    死鬼说的很有道理,用rowfilter