0

我想在数据列表中绑定选中的复选框行..使用此代码但不绑定所有记录..

System.Web.UI.WebControls.CheckBox chkb = new System.Web.UI.WebControls.CheckBox();
string chkBoxIndex = string.Empty;

foreach (C1.Web.Wijmo.Controls.C1GridView.C1GridViewRow row in C1GridView1_listview.Rows)
{
    chkBoxIndex = (string)C1GridView1_listview.DataKeys[row.RowIndex].Value.ToString();
    chkb = (System.Web.UI.WebControls.CheckBox)row.FindControl("chkBxSelect");

    if (chkb.Checked == true)
    {

        cmd = new SqlCommand("select sample_code,convert(varchar(10),purchase_date,101) as purchase_date,sample_image_id,sample,image,style,descript from sample_shopping,sample_image where sample_shopping.sample_code=sample_image.sample and type='IMG' and sample_code='" + chkBoxIndex + "'", con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows)
        {
            dr.Close();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            dl_search.DataSource = dt;
            dl_search.DataBind();
            con.Close();

        }
    }
4

1 回答 1

0

仅在 foreach 循环中绑定数据列表。这就是为什么它每次在数据列表中显示一条记录。使数据列表绑定出 foreach 循环,如

System.Web.UI.WebControls.CheckBox chkb = new System.Web.UI.WebControls.CheckBox();
string chkBoxIndex = string.Empty;

    foreach (C1.Web.Wijmo.Controls.C1GridView.C1GridViewRow row in C1GridView1_listview.Rows)
    {
        chkBoxIndex = (string)C1GridView1_listview.DataKeys[row.RowIndex].Value.ToString();
        chkb = (System.Web.UI.WebControls.CheckBox)row.FindControl("chkBxSelect");

        if (chkb.Checked == true)
        {
          //update the table with update query like
           //update <table-name> set check='true' where sno=chkBoxIndex or insert the select records into seperate table.
           ...
        }
    }

现在使用如下查询将数据绑定到数据列表

 select * from <tablename> or where check=true

这样只有选定的行将显示在 datalist 中。希望这会有所帮助。

于 2012-06-25T20:55:22.913 回答