Search code examples
c#asp.netgridviewtemplatefield

how do I add a CheckBox to a TemplateField HEADER?


I have this:

<asp:GridView ID="gvShows" runat="server" HorizontalAlign="Center" DataKeyNames="dataSource,title" Caption="Show List" AutoGenerateColumns="False" AllowSorting="True" CaptionAlign="Top" OnSorting="gvShows_Sorting">
<RowStyle BorderColor="Black" />
<Columns> 
    <asp:TemplateField HeaderText="Select" > 
        <ItemTemplate> 
            <asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false"/> 
        </ItemTemplate> 
    </asp:TemplateField> 
    <asp:BoundField HeaderText="Data Source" DataField="DataSource" SortExpression="DataSource"/> 
    <asp:BoundField HeaderText="Show Title" DataField="Title" SortExpression="Title"/> 
    <asp:BoundField HeaderText="Episode Title" DataField="EpisodeTitle"     SortExpression="EpisodeTitle"/> 
    <asp:BoundField HeaderText="Genre" DataField="Genre" SortExpression="Genre"/> 
    <asp:BoundField HeaderText="Show Type Description" DataField="ShowTypeDescription" SortExpression="ShowTypeDescription"/> 
    <asp:BoundField HeaderText="Director Name" DataField="DirectorName" SortExpression="DirectorName"/> 
    <asp:BoundField HeaderText="Release Year" DataField="ReleaseYear" SortExpression="ReleaseYear"/> 
    <asp:BoundField HeaderText="Season Episode" DataField="SeasonEpisode" SortExpression="SeasonEpisode"/> 
    <asp:BoundField HeaderText="Show ID" DataField="ShowId" SortExpression="ShowId"/> 
    <asp:BoundField HeaderText="Episode ID" DataField="EpisodeID" SortExpression="EpisodeID"/> 
</Columns>  

Which gives me this:

I want to change where the highlighted word "SELECT" is to an actual CheckBox so that when the user checks it, it checks all boxes under.

How do I go about modifying the header text from "Select" to an actual CheckBox?


Solution

  • You can use Header Template to achieve this and remove the HeaderText from the Template field

     <asp:TemplateField > 
      <ItemTemplate> 
         <asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false"/> 
       </ItemTemplate> 
    
      <HeaderTemplate>
        <asp:CheckBox ID="chkBxHeader" runat="server" />
        </HeaderTemplate>
     </asp:TemplateField>