Click here to Skip to main content
12,952,778 members (34,251 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi i have tried the following code on my button click event but it shows an error as Control 'ctl00_ContentPlaceHolder1_grdsalarydetail' of type 'GridView' must be placed inside a form tag with runat=server. i have tried the same logic of code i with vb which works after adding an overrides method as

        Response.AddHeader("Content-disposition", "Attachmen;filename=reports.xlsx");
        StringWriter StringWriter = new System.IO.StringWriter();
        HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);

but can any one explain in csharp how can i use the same code as i am not getting the correct logic of override method in csharp

<b><pre lang="vb">Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
    End Sub</pre>
Posted 3-Dec-12 4:30am
Updated 3-Dec-12 4:34am
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

private void ExportGridToExcel(GridView grdGridView, string fileName)
string.Format("attachment;filename={0}.xlsx", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.xlsx";

StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

//You have to override this method
public override void VerifyRenderingInServerForm(Control control)

<pre lang="c#">
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Actually, there are a lot of solutions to export data from gridview to Excel. I am afraid people may not successfully reproduce your problem, so why do not try other solutions? they are easy to perform and at the same time, you can learn much from them. Here I can suggest you some, I believe they are helpful for you, you want simple code, so you can decide yourself which is the best
9 Solutions to Export Data to Excel for ASP.NET[^]
Export DataTable to Excel through DataGridView[^]
Excel to Datatable and Datatable to Excel[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,389
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,920

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100