Solved

Allow Paging on working GridView export

Posted on 2011-09-15
3
275 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now