Link to home
Start Free TrialLog in
Avatar of zintech
zintech

asked on

Allow Paging on working GridView export

I have the following code that works to export a GridView to Excel:

HttpContext.Current.Response.Clear();

        HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
        HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);


        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "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.Response.Write(sw.ToString());
                HttpContext.Current.Response.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.
Avatar of Rick
Rick

Try changing this:

  //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);
                }


To this:
//add each of the data rows to the table

                gv.AllowPaging = false;
                foreach (GridViewRow row in gv.Rows)
                {
                    
                    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);
                }
                gv.AllowPaging = true;

Open in new window

Avatar of zintech

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
Avatar of Rick
Rick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial