Select all Gridview record using CheckBox in the Gridview header
<p>I am using checkbox in the gridview header to select all records in the gridview. this is the code behind method.</p>

<pre><code>&lt;asp:GridView ID="gvP" runat="server" AutoGenerateColumns="false" AllowPaging="false"
AllowSorting="false" DataKeyNames="PmtId" CssClass="list-table" HeaderStyle-CssClass="header"
EnableModelValidation="True"&gt;
    &lt;Columns&gt;
    &lt;asp:BoundField DataField="PmtId" HeaderText="PmtId" ReadOnly="True" Visible="false" /&gt;
    &lt;asp:TemplateField HeaderText="All" ItemStyle-CssClass="checkbox-col" HeaderStyle-CssClass="checkbox-col"&gt;
    &lt;HeaderStyle HorizontalAlign="Center" /&gt;
    &lt;HeaderTemplate&gt;
    &lt;input id="chkBoxAll" type="checkbox" onclick='javascript:checkAllBoxes("CLEAR_PT")' /&gt;
    &lt;/HeaderTemplate&gt;
    &lt;ItemTemplate&gt;
    &lt;asp:CheckBox runat="server" ID="chkSelect" /&gt;
    &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;asp:TemplateField HeaderText="S/No." ItemStyle-CssClass="seq-col" HeaderStyle-CssClass="seq-col"&gt;
    &lt;ItemTemplate&gt;
    &lt;%# Eval("SNo")%&gt;
    &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;/Columns&gt;
&lt;/asp:GridView&gt; 

protected void chkSelectAll(string arg)
{
    if (arg.Equals("CLEAR_PT"))
    {
    CheckBox chkAll = gvP.HeaderRow.FindControl("chkBoxAll") as CheckBox;
    if (chkAll.Checked == true)
    {
    foreach (GridViewRow gvRow in gvP.Rows)
    {
    CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect") as CheckBox;
    chkSel.Checked = true;
    }
    }
    else
    {
    foreach (GridViewRow gvRow in gvP.Rows)
    {
    CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect") as CheckBox;
    chkSel.Checked = false;
    }
    }
    }
}
</code></pre>

<p>I am getting 'Object reference not set to an instance of an object' run time error in the following line.</p>

<pre><code>if (chkAll.Checked == true)
</code></pre>

<p>Any Idea?</p>
