Saturday, 25 September 2010

GridView and export to excel

This is very simple to implement in ASP.NET. But, there are possibilities to get problems in exporting to excel from grid view. When you bind data to gridview and write some logic to export to excel then it will not be enough. We have to check or write some additional logic which will help us to solve the problems. Below is the explanation for all problems we may get in the complete process along with detailed solution.

You may encounter below errors when you try to implement the export to excel for gridview.
1. Control of type "GridView" must be placed inside of the form tag with runat="server"
This is very well known error to ASP.NET developers and by seeing it, we think that the control is not inside the form with runat server. But this is not correct. This error will come even if we put the GridView inside form with runat server. The reason is, in the export to excel logic we are calling RenderControl() method of GridView. So, to render it without any issues we have to override the "VerifyRenderingInServerForm" in our code.  Below is the syntax of the event. Add this to the c# code in code behind file. Remember this event is a Page event means this method you should place in ASPX page. If you are using user control to implement this export to excel logic then below are the ways to go.
1. If your user control is using by less than 3-4 pages then go to each and every page and add this event to the page.
2. If your user control is using by more than 5 pages then the best solution is to create a base page [Which inherits from System.Web.UI.Page class] and all your ASPX pages should inherit from this base page.

public override void VerifyRenderingInServerForm(Control control)
{
//Confirms that an HtmlForm control is rendered for the specified ASP.NET
//server control at run time.
}
Now, after we added the event to page, the error will go away for sure.

2. Even when you add the above code/event to the page, this is not enough if you have the paging, sorting enabled on gridview. If you enable paging or sorting then you encounter the below error.

"RegisterForEventValidation can only be called during Render();"

This error is coming because we are doing paging and sorting. If no paging or sorting enabled this error will not come. To resolve this error, please follow below steps.
1. In your export to excel button click event, first disable the paging, sorting on gridview and do data bind.
2. Call export to excel logic.
3. Re-enable paging, sorting on gridview and databind.
Below is the logic we have to use in export to excel button click event.
gvReport.AllowPaging = false;
gvReport.AllowSorting = false;
gvReport.DataBind();
ExportToExcel();//Method to use export to excel.
gvReport.AllowPaging = true;
gvReport.AllowSorting = false;
gvReport.DataBind();
Now, you are clear with all errors and the logic will export all data in gridview to excel.
FYI, I am placing a sample of ExportToExcel() functionality here.
private void ExportToExcel()
{
Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment;filename=excel_report.xls"));
Response.Charset = "";

// Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvReport.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
Note: gvReport is the gridview control name in my example. And there are lot of posts in internet guide you incorrect to resolve the above error. For example, they will say set EnableEventValidation="false" in the <%@ Page directive Or disable validation in web.config level to resolve the error. Please do not set it.

Hope this helps to solve all problems you are facing.

No comments:

Post a Comment