zintech
asked on
Allow Paging on working GridView export
I have the following code that works to export a GridView to Excel:
HttpContext.Current.Respon se.Clear() ;
HttpContext.Current.Respon se.Cache.S etExpires( DateTime.U tcNow.AddD ays(-1));
HttpContext.Current.Respon se.Cache.S etValidUnt ilExpires( false);
HttpContext.Current.Respon se.Cache.S etRevalida tion(HttpC acheRevali dation.All Caches);
HttpContext.Current.Respon se.AddHead er(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Respon se.Content Type = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = new Table();
table.GridLines = gv.GridLines;
// add the header row to the table
if (gv.HeaderRow != null)
{
GridViewRow temp = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal );
TableCell tempcell = new TableCell();
TableCell tempcell2 = new TableCell();
tempcell.Text = "WBS";
temp.Cells.Add(tempcell);
tempcell2.Text = "WBSTitle";
temp.Cells.Add(tempcell2);
table.Rows.Add(temp);
}
//add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
gv.AllowPaging = true;
GridViewRow temp = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal );
TableCell tempcell = new TableCell();
TableCell tempcell2 = new TableCell();
tempcell.Text = row.Cells[3].Text;
tempcell2.Text = row.Cells[4].Text;
temp.Cells.Add(tempcell);
temp.Cells.Add(tempcell2);
table.Rows.Add(temp);
}
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Respon se.Write(s w.ToString ());
HttpContext.Current.Respon se.End();
}
}
The issue I am having is that the GridView includes paging. However, the above code only displays the first page in a series of 4 pages on a Gridview. I cannot get the second through fourth pages to export to Excel.
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
"content-disposition", string.Format("attachment;
HttpContext.Current.Respon
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = new Table();
table.GridLines = gv.GridLines;
// add the header row to the table
if (gv.HeaderRow != null)
{
GridViewRow temp = new GridViewRow(0, 0, DataControlRowType.Header,
TableCell tempcell = new TableCell();
TableCell tempcell2 = new TableCell();
tempcell.Text = "WBS";
temp.Cells.Add(tempcell);
tempcell2.Text = "WBSTitle";
temp.Cells.Add(tempcell2);
table.Rows.Add(temp);
}
//add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
gv.AllowPaging = true;
GridViewRow temp = new GridViewRow(0, 0, DataControlRowType.Header,
TableCell tempcell = new TableCell();
TableCell tempcell2 = new TableCell();
tempcell.Text = row.Cells[3].Text;
tempcell2.Text = row.Cells[4].Text;
temp.Cells.Add(tempcell);
temp.Cells.Add(tempcell2);
table.Rows.Add(temp);
}
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Respon
HttpContext.Current.Respon
}
}
The issue I am having is that the GridView includes paging. However, the above code only displays the first page in a series of 4 pages on a Gridview. I cannot get the second through fourth pages to export to Excel.
ASKER
Unfortunately adding the Paging properties to the top and bottom of the loop did not help. It still only does the first page of all of the pages
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
//add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
gv.AllowPaging = true;
GridViewRow temp = new GridViewRow(0, 0, DataControlRowType.Header,
TableCell tempcell = new TableCell();
TableCell tempcell2 = new TableCell();
tempcell.Text = row.Cells[3].Text;
tempcell2.Text = row.Cells[4].Text;
temp.Cells.Add(tempcell);
temp.Cells.Add(tempcell2);
table.Rows.Add(temp);
}
To this:
Open in new window