title: Remove Checkboxes while exporting Gridview to Excel
body: <p>I have a gridview that needs to be exported to Excel. I have managed to remove the Checkboxes from Rows but don't know how to remove from the Header and also delete the Checkbox Column entirely. Thanks for help.</p>
  
  <pre><code>    &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Id"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Select" &gt;
              &lt;HeaderTemplate&gt;
                  &lt;input id="chkAll" onclick="checkAll(this);" runat="server" type="checkbox" /&gt;
              &lt;/HeaderTemplate&gt;
              &lt;ItemTemplate&gt;
              &lt;asp:CheckBox  ID="CheckBox1" runat="server" /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:BoundField DataField="Event" HeaderText="Event" SortExpression="Event" /&gt;
          &lt;asp:BoundField DataField="Event_Description" HeaderText="Event_Description" SortExpression="Event_Description" /&gt;
          &lt;asp:BoundField DataField="Start_Date" HeaderText="Start_Date" SortExpression="Start_Date" /&gt;
      &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Here's the backend C# code to export to excel.</p>
  
  <pre><code>protected void download_Click(object sender, EventArgs e)
  {
      PrepareGridViewForExport(GridView1);
      Response.ClearContent(); 
      Response.AddHeader("content-disposition", "attachment; filename=GridViewToExcel.xls"); 
      Response.ContentType = "application/excel"; 
      StringWriter sWriter = new StringWriter();
      HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
      GridView1.RenderControl(hTextWriter);
      Response.Write(sWriter.ToString());
      Response.End();
  }
  public override void VerifyRenderingInServerForm(Control control)
  {
  }
  private void PrepareGridViewForExport(Control gv)
  {
      Literal l = new Literal();
      string name = String.Empty;
      for (int i = 0; i &lt; gv.Controls.Count; i++)
      {
          if (gv.Controls[i].GetType() == typeof(CheckBox))
          {
              gv.Controls.Remove(gv.Controls[i]);
              gv.Controls.AddAt(i, l);
          }
          if (gv.Controls[i].HasControls())
          {
              PrepareGridViewForExport(gv.Controls[i]);
          }
      }
  }
  </code></pre>