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.
<asp:ImageButton ID="ExportBtn" runat="server" ImageUrl="../Images/ExcelIcon.jpg" Alt="Export" Width="40px" Height="40px" style="border-width:0px" onclick="ExportBtn_Click" />
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();
}
The error I get when I try to export all pages is: Server Error in '...20150605_0846_local' Application.
'DocStatusDropDown' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
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.
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");