繁体   English   中英

在C#中将GridView列动态绑定到CheckBoxList

[英]Binding GridView Columns to CheckBoxList Dynamically in C#

我只想将Table Columns绑定到CheckBoxList ,然后选定的列应显示在GridView

到目前为止,我的代码是:

public void BindCheckBoxList(DataSet ds) 
{ 
    int i = 0; 
    foreach (DataColumn dc in ds.Tables[0].Columns) 
    { 
        ListItem li = new ListItem(dc.ToString(), i.ToString());   
        CheckBoxList1.Items.Add(li); i++; 
    } 
}
protected void mainGridView_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow &&
             (e.Row.RowState == DataControlRowState.Normal ||
              e.Row.RowState == DataControlRowState.Alternate))
           {

               if (e.Row.Cells[1].FindControl("selectCheckbox") == null)
               {
                   CheckBox selectCheckbox = new CheckBox();
                   //Give id to check box whatever you like to
                   selectCheckbox.ID = "newSelectCheckbox";
                   e.Row.Cells[1].Controls.Add(selectCheckbox);

               }
           }
       }

我认为您使用此代码。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="HobbyId">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="chkSelect" runat="server" Checked='<%# Eval("IsSelected") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Hobby" HeaderText="Hobby" ItemStyle-Width="150px" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="Save" />

在aspx.cs代码中

private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT [HobbyId], [Hobby], [IsSelected] FROM Hobbies"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}

这是一个基于CheckBoxList隐藏GridView列的完整示例。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //fill the datatable from the database
            DataTable dt = //fill the table here...

            //bind the table to the grid
            GridView1.DataSource = dt;
            GridView1.DataBind();

            //loop all the datatable columns to fill the checkboxlist
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                CheckBoxList1.Items.Insert(i, new ListItem(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName, true));
            }
        }
    }

    protected void CheckBoxList1_TextChanged(object sender, EventArgs e)
    {
        //loop all checkboxlist items
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected == true)
            {
                //loop all the gridview columns
                for (int i = 0; i < GridView1.Columns.Count; i++)
                {
                    //check if the names match and hide the column
                    if (GridView1.HeaderRow.Cells[i].Text == item.Value)
                    {
                        GridView1.Columns[i].Visible = false;
                    }
                }
            }
        }
    }

在.aspx页面上

<asp:CheckBoxList ID="CheckBoxList1" runat="server" OnTextChanged="CheckBoxList1_TextChanged" AutoPostBack="true"></asp:CheckBoxList>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="field01" HeaderText="Column A" />
        <asp:BoundField DataField="field02" HeaderText="Column B" />
        <asp:BoundField DataField="field03" HeaderText="Column C" />
        <asp:BoundField DataField="field04" HeaderText="Column D" />
        <asp:BoundField DataField="field05" HeaderText="Column E" />
    </Columns>
</asp:GridView>

请注意, AutoGenerateColumns设置为false。 如果它们是自动生成的,则这些列不属于GridView列集合。

当然, HeaderText="xxx"必须与存储在DataTable中的数据库中的列名匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM