Solved

Allow Paging on working GridView export

Posted on 2011-09-15
3
278 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:zintech
  • 2
3 Comments
 
LVL 13

Expert Comment

by:Rick
ID: 36544603
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

0
 

Author Comment

by:zintech
ID: 36545004
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
0
 
LVL 13

Accepted Solution

by:
Rick earned 500 total points
ID: 36545800
Try setting gv.AllowPaging = false before you call the export method.
Then gv.AllowPaging = true after executing the export method.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question