rofans91
rofans91

Reputation: 3010

Exporting GridView to XLS --- Paging Issue

I have a GridView and I intend to export it into .xls file. Paging is enabled in this gridview. The codes I am currently using can only export the first page of the gridview.

   Response.Clear();
   Response.Buffer = true;

   Response.AddHeader("content-disposition","attachment;filename=DataTable.xls");
   Response.Charset = "";
   Response.ContentType = "application/vnd.ms-excel";

   StringWriter sw = new StringWriter();
   HtmlTextWriter hw = new HtmlTextWriter(sw);

   //As you notice, below I tried to disable the paging yet it's unsuccessful
   //FYI I am able to really prevent first column, header row, and footer row to
   //be exported through this

   gvGridView.Columns[0].Visible = false;
   gvGridView.HeaderRow.Visible = false;
   gvGridView.FooterRow.Visible = false;
   gvGridView.AllowPaging = false;

   for (int i = 0; i < gvGridView.Rows.Count; i++)
   {
       gvGridView.Rows[i].Attributes.Add("class", "textmode");
   }
   gvGridView.RenderControl(hw);

   string style = @"<style> .textmode { mso-number-format:\@; } </style>";
   Response.Write(style);
   Response.Output.Write(sw.ToString());
   Response.Flush();
   Response.End();

I noticed this question was previously asked yet there was no correct answer.

Any help is appreciated.

Thank's

Upvotes: 2

Views: 3788

Answers (2)

Gustavo F
Gustavo F

Reputation: 2206

Just make a GridView.DataBind() after change the AllowPaging property.

Upvotes: 1

Brissles
Brissles

Reputation: 3891

You should rebind your GridView after you specify gvGridView.AllowPaging = false;, then export. Otherwise .RenderControl(hw); will only be rendering the currently selected GridView page.

Upvotes: 2

Related Questions