简体   繁体   中英

Add Checkbox to header of gridview

I have following gridview:

在此输入图像描述

I wanted to put checkbox to header of this grid, means below or besides Maths,Physics,Chemistry,Biology Header Text.

Code for Grid:

<asp:GridView ID="GvSearch" runat="server" CellPadding ="3"
                                Width="100%" AutoGenerateColumns="False">
           <Columns>
               <asp:TemplateField>
                   <ItemTemplate>
                       <asp:Label ID="lblCity" runat="server"  Text='<%# Bind("City") %>' ></asp:Label>
                       <asp:CheckBox ID="ChkCity" runat="server" />
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Maths">

                   <ItemTemplate>
                       <asp:Label ID="lblMaths" runat="server" Text='<%# Bind("Maths") %>'></asp:Label>

                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Physics">

                   <ItemTemplate>
                       <asp:Label ID="lblPhysics" runat="server" Text='<%# Bind("Physics") %>'></asp:Label>

                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Chemistry">

                   <ItemTemplate>
                       <asp:Label ID="lblChemistry" runat="server" Text='<%# Bind("Chemistry") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Biology">
                   <ItemTemplate>
                       <asp:Label ID="lblBio" runat="server" Text='<%# Bind("Biology") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
       </Columns>
         </asp:GridView>

I tried adding it as:

<asp:TemplateField HeaderText="Physics">
                   <HeaderTemplate>
                   <asp:CheckBox ID="ChkCity" runat="server" />
                   </HeaderTemplate>
                   <ItemTemplate>
                       <asp:Label ID="lblPhysics" runat="server" Text='<%# Bind("Physics") %>'></asp:Label>

                   </ItemTemplate>

But this didnt worked.

Please help me to add checkbox to header of gridview.

Not sure, but I think the HeaderText property in TemplateField might be causing problems with the HeaderTemplate . Looking at a code sample on MSDN , they don't use HeaderText when using the HeaderTemplate . Try something like this (not tested):

<asp:TemplateField>
    <HeaderTemplate>
        <asp:CheckBox ID="ChkCity" Text="Physics" runat="server" />
    </HeaderTemplate>
    <ItemTemplate>
        ....
    </ItemTemplate>
</asp:TemplateField>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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