Solved

Allow Paging on working GridView export

Posted on 2011-09-15
3
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Easy filter aspnet 2 34
JQuery Detecting page post back 6 19
Unlocking a column in excel using C# 17 27
Powershell form - Anchor a button to a dynamically sized groupbox 2 42
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

740 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