Mohamed Abdelrahman
Mohamed Abdelrahman

Reputation: 33

How to export all grid data to excel?

when i add the below code working fine but it only export data from Gridview in one page not all data if it enable paging ,

please give me your advise :)

    protected void BtnExport_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment; filename=Off-Board.xls");
        Response.ContentType = "application/excel";

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        GridView_Report.RenderControl(htmlTextWriter);
        Response.Write(stringWriter.ToString());
        Response.End();

    }
enter code here

Upvotes: 0

Views: 75

Answers (2)

user5341897
user5341897

Reputation:

After Export
GridView_Report.AlloPaging=True;
GrridView_Report.DataSource=Your_datasource;

GridView_Report.DataBind();

Upvotes: 2

A_Sk
A_Sk

Reputation: 4630

try including

GridView_Report.AllowPaging=False;
GridView_Report.DataSource=your_datasource;
GridView_Report.DataBind();

and after export, set

GridView_Report.AllowPaging=True;
GridView_Report.DataSource=your_datasource;
GridView_Report.DataBind();

in your case

protected void BtnExport_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment; filename=Off-Board.xls");
        Response.ContentType = "application/excel";

        //here

GridView_Report.AllowPaging=False;
    GridView_Report.DataSource=your_datasource;
    GridView_Report.DataBind();

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        GridView_Report.RenderControl(htmlTextWriter);
        Response.Write(stringWriter.ToString());
        Response.End();

GridView_Report.AllowPaging=True;
    GridView_Report.DataSource=your_datasource;
    GridView_Report.DataBind();

    }

Upvotes: 1

Related Questions