如何在asp.net webform Gridview中显示Checkbox

时间:2016-10-10 10:52:05

标签: c# asp.net gridview checkbox

我想在Gridview中显示Checkbox,

但现在,它只在Gridview中显示动态数据,

未在Gridview中显示动态复选框。

如何解决问题,谢谢。

.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
{

    using (var conn = new SqlConnection(strConn))
    {
        try
        {
            conn.Open();

            ckloginConnectionString.SelectCommand = @"select a,b,c,d ,e from testTable where a =@a ";
            ckloginConnectionString.SelectParameters.Add("a", a);
             //==================
                DataView dv = (DataView)ckloginConnectionString.Select(new DataSourceSelectArguments());
                GridView1.DataSource = dv;
                GridView1.DataBind();

                //==================



                ckloginConnectionString.DataBind();
                ckloginConnectionString.DataBind();

        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
}

的.aspx

          <div class="table-wrapper">
    <asp:GridView runat="server" ID="GridView1" CssClass="alt" DataKeyNames="InsertedDate,Upload_Schoo_No" AutoGenerateColumns="False" OnRowCommand="grvclscour_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="check"   InsertVisible="false" ItemStyle-BorderStyle="Double">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Visible="true" />
                </ItemTemplate>
            </asp:TemplateField>
              <asp:BoundField DataField="a" HeaderText="a">
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="b" HeaderText="b">
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="c" HeaderText="c">
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>

            <asp:ButtonField CommandName="d" HeaderText="d">
                <ItemStyle HorizontalAlign="Center" />
            </asp:ButtonField>
            <asp:ButtonField CommandName="e" HeaderText="e">
                <ItemStyle HorizontalAlign="Center" />
            </asp:ButtonField>
        </Columns>
    </asp:GridView>
</div>

1 个答案:

答案 0 :(得分:1)

Eval会做你想要的。

像这样改变你的网格设计:

 <asp:GridView runat="server" ID="GridView1" CssClass="alt" DataKeyNames="InsertedDate,Upload_Schoo_No" AutoGenerateColumns="False" OnRowCommand="grvclscour_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="check"   InsertVisible="false" ItemStyle-BorderStyle="Double">
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" Visible="true" Checked='<%# Eval("YourDataField") %>' />
            </ItemTemplate>
        </asp:TemplateField>
          <asp:BoundField DataField="a" HeaderText="a">
            <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>
        <asp:BoundField DataField="b" HeaderText="b">
            <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>
        <asp:BoundField DataField="c" HeaderText="c">
            <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>

        <asp:ButtonField CommandName="d" HeaderText="d">
            <ItemStyle HorizontalAlign="Center" />
        </asp:ButtonField>
        <asp:ButtonField CommandName="e" HeaderText="e">
            <ItemStyle HorizontalAlign="Center" />
        </asp:ButtonField>
    </Columns>
</asp:GridView>