Solving GridView Export Paging Problems

Few days ago there was a question asked on www.asp.net forums that how can I export the whole GridView to Excel if the paging is enabled. I prepared this small example which shows how to achieve the task. Please note that this example is implemented in Visual Studio.NET 2005 Professional.

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO;

public partial class EXportTest : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)     {        if (!Page.IsPostBack)         {             BindData;          }

    }        private string ConnectionString     {        get { return @"Server=localhost;Database=Northwind;         Trusted_Connection=true"; }

    }        private void BindData     {        // make the query         string query = "SELECT * FROM Categories";         SqlConnection myConnection = new SqlConnection(ConnectionString);         SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);         DataSet ds = new DataSet;         ad.Fill(ds, "Categories");         GridView1.DataSource = ds;         GridView1.DataBind;         /* It's a good idea if you can Cache the DataSet */    }    public override void VerifyRenderingInServerForm(Control control)     {        // Confirms that an HtmlForm control is rendered for        the specified ASP.NET server control at run time.

    }    protected void Button1_Click(object sender, EventArgs e)     {         Response.Clear;

        Response.AddHeader("content-disposition",         "attachment;filename=FileName.xls");

        Response.Charset = "";        // If you want the option to open the Excel file without saving than

        // comment out the line below

        // Response.Cache.SetCacheability(HttpCacheability.NoCache);        Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter;

        System.Web.UI.HtmlTextWriter htmlWrite =        new HtmlTextWriter(stringWrite);        // turn off paging         GridView1.AllowPaging = false;         BindData;          

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString);

        Response.End;        // turn the paging on again         GridView1.AllowPaging = true;         BindData;              }       protected void GridView1_PageIndexChanging(object sender,     GridViewPageEventArgs e)     {         GridView1.PageIndex = e.NewPageIndex;         BindData;      } }  

This article is part of the GWB Archives. Original Author: Mohammad Azam

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.