简体   繁体   English

如何在 ASP.NET 的 GridView 单元格中添加复选框

[英]How to add checkboxes in GridView Cells on ASP.NET

So I am new to ASP.NET, and am having trouble finding this on the web too.所以我是 ASP.NET 的新手,在网上也找不到这个。 What I am trying to accomplish is adding checkboxes inside the cells of the table I would like to create.我想要完成的是在我想创建的表格的单元格中添加复选框。 Currently, the code I have implemented presents a whole new check box column, as show below:目前,我实现的代码呈现了一个全新的复选框列,如下所示:

<asp:GridView runat="server" ID="Gv1">
        <Columns>
            <asp:templatefield HeaderText="Check Box">
                <itemtemplate>
                    <asp:checkbox ID="cb" runat="server"></asp:checkbox>
                </itemtemplate>
            </asp:templatefield>
        </Columns>
    </asp:GridView>

Again, what I am trying to accomplish is make each of the empty cells on the grid consist of a check box.同样,我想要完成的是使网格上的每个空单元格都包含一个复选框。 Any help would be greatly appreciated.任何帮助将不胜感激。

You don't share how many columns you need?你不分享你需要多少列?

But, lets say we want 20 columns, and 10 rows deep.但是,假设我们想要 20 列和 10 行深。

So, we have this markup:所以,我们有这个标记:

<asp:GridView ID="GridView1" runat="server" CssClass="table"
  OnRowDataBound="GridView1_RowDataBound">
</asp:GridView>

And our code to load is this:我们要加载的代码是这样的:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadGrid();
    }

    void LoadGrid()
    {
        DataTable rstData = new DataTable();
        //ADD 20 columns
        int i;
        int j;
        for (i = 1;i <= 20;i++)
            rstData.Columns.Add("C" + i,typeof(bool));

        // now add 10 rows
        for (i = 1;i <= 10;i++)
        {
            DataRow OneRow = rstData.NewRow();
            // lets check the first 5 columns
            for (j = 0; j < OneRow.Table.Columns.Count; j++)
                OneRow[j] = (j <= 4);
            
            rstData.Rows.Add(OneRow);
        }
        GridView1.DataSource = rstData;
        GridView1.DataBind();
    }

We VERY unfortunately have to deal with default check boxes on a grid are read only and not enabled.很遗憾,我们必须处理网格上的默认复选框是只读的且未启用。 So, in the on- row data bound even, we have to add this:因此,即使在行数据绑定中,我们也必须添加以下内容:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (TableCell c in e.Row.Cells)
            {
                CheckBox ck = c.Controls[0] as CheckBox;
                ck.Enabled = true;
            }
        }
    }

And now we get this:现在我们得到了这个:

在此处输入图像描述

Edit: Example with check box(s) from data table.编辑:数据表中带有复选框的示例。

So, if you need to setup some check boxes in the gv, then I suggest you actually add the check boxes as columns and use REAL asp.net check box controls.因此,如果您需要在 gv 中设置一些复选框,那么我建议您实际将复选框添加为列并使用 REAL asp.net 复选框控件。

So, with this gv markup:所以,使用这个 gv 标记:

<asp:GridView ID="GridView1" runat="server" class="table" 
    AutoGenerateColumns="false" DataKeyNames="ID"  >
    <Columns>
        <asp:BoundField DataField="FirstName"   HeaderText="FirstName"  />
        <asp:BoundField DataField="LastName"    HeaderText="LastName"   />
        <asp:BoundField DataField="City"        HeaderText="City"       />
        <asp:BoundField DataField="HotelName"   HeaderText="HotelName" HeaderStyle-Width="200"   />
        <asp:BoundField DataField="Description" HeaderText="Description" />

        <asp:TemplateField HeaderText="Active"  ItemStyle-HorizontalAlign="Center"  >
            <ItemTemplate>
                <asp:CheckBox ID="chkActice" runat="server"
                    Checked='<%# Eval("Active") %>' />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Smoking"  ItemStyle-HorizontalAlign="Center"  >
            <ItemTemplate>
                <asp:CheckBox ID="chkSmoking" runat="server"
                    Checked='<%# Eval("Smoking") %>' />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Balcony"  ItemStyle-HorizontalAlign="Center"  >
            <ItemTemplate>
                <asp:CheckBox ID="chkBalcony" runat="server" 
                    Checked='<%# Eval("Balcony") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And now our code behind to load above is this:现在我们在上面加载的代码是这样的:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadGrid();
    }
    void LoadGrid()
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string strSQL = "SELECT * FROM tblHotelsA ORDER BY HotelName ";
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                conn.Open();
                DataTable rst = new DataTable();
                rst.Load(cmdSQL.ExecuteReader());
                GridView1.DataSource = rst;
                GridView1.DataBind();
            }
        }
    }

And now we see/get this:现在我们看到/得到这个:

在此处输入图像描述

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

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