protected void Export2Excel()
{
try
{
this.GridView1.AllowPaging = false;
this.GridView1.AllowSorting = false;
this.GridView1.EditIndex = -1;
this.BindData();
Response.Clear();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition",
"attachment;filename=MyList.xls");
Response.Charset = "";
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
GridView1.RenderControl(hwriter);
Response.Write(swriter.ToString());
Response.End();
}
catch (Exception exe)
{
throw exe;
}
}
use this code and try it it will work for me...
and also take care that you need not put your grid in side a update panel or it will resulted in error like this...
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.