title: How to export all pages of gridview to excel without columns containing controls?
body: <p>I have a gridview that has 2 columns that are a dropdownlist and upload control. I am trying to export the gridview to excel and the code works but it only works to export the current view not all the pages. When I try to export all pages the data binding fails and code crashes. Please help if you know a way of getting all the pages exported to excel/csv with or without the columns containing controls. </p>
  
  <pre><code>&lt;asp:ImageButton ID="ExportBtn" runat="server" ImageUrl="../Images/ExcelIcon.jpg" Alt="Export" Width="40px" Height="40px" style="border-width:0px" onclick="ExportBtn_Click" /&gt;
  
  
  protected void ExportBtn_Click(object sender, ImageClickEventArgs e)
  {
  
          gv.Columns[7].Visible = false;
          gv.Columns[8].Visible = false;
  
          Response.ClearContent();
          Response.AppendHeader("content-disposition", "attachment; filename=Documents.xls");
          Response.ContentType = "application/excel";
          gv.AllowPaging = false;
          gv.DataBind();
  
          StringWriter sw = new StringWriter();
          HtmlTextWriter htw = new HtmlTextWriter(sw);
  
          Response.Charset = String.Empty;
  
          gv.RenderControl(htw);
          Response.Write(sw.ToString());
  
          Response.End();
      }
  </code></pre>
  
  <p>The error I get when I try to export all pages is:
  Server Error in '...20150605_0846_local' Application.</p>
  
  <p>'DocStatusDropDown' has a SelectedValue which is invalid because it does not exist in the list of items.
  Parameter name: value</p>
  
  <p>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
  
  <pre><code>Exception Details: System.ArgumentOutOfRangeException: 'DocStatusDropDown' has a SelectedValue which is invalid because it does not exist in the list of items.
  Parameter name: value
  
  Source Error: 
  
  
  Line 194:        gvMainGrid.Columns[8].Visible = false;
  Line 195:        gvMainGrid.AllowPaging = false;
  Line 196:        gvMainGrid.DataBind();
  Line 197:        Response.ClearContent();
  Line 198:        Response.AppendHeader("content-disposition", "attachment; filename=MissingDocuments.xls");
  </code></pre>