2

I have following gridview:

enter image description here

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.

1 Answer 1

7

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Its rubbing my header text, i want headertext also
@M.N.S. - You set the text in the CheckBox control using the Text property. Is that not working (i.e., the Text isn't showing up)?
ohh,let me try that one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.