user1490374
user1490374

Reputation: 65

How to export gridview to excel sheet with paging

I've tried using the radiobuttonlist to let the user choose if he/she wants to export either the current page of the gridview or the whole gridview to an excel sheet, but only the current page one's working where as for exporting the whole gridview to an excel sheet, it only shows the header rows. What's the problem here, is it something to do with the selectedindex of the radiobuttonlist?

protected void btnExport_Click(object sender, EventArgs e)
{
    if (this.RadioButtonList1.SelectedIndex == 1)
    {
        //  the user wants all rows in the current page, set pagesize and rebind
        this.GridView1.PageSize = 10;
        this.GridView1.DataBind();
    }
    else if (this.RadioButtonList1.SelectedIndex == 2)
    {
        //  the user wants all rows exported, have to turn off paging and rebind
        this.GridView1.AllowPaging = false;
        this.GridView1.DataBind();
    }
    GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1);
}

Upvotes: 1

Views: 6802

Answers (2)

Amol Kolekar
Amol Kolekar

Reputation: 2325

You can Use following code...Use Linq to bind specific records from Datasource to the Grid view while paging else bind whole Datasource..then use Gridview to your Export method.. This should work...

protected void btnExport_Click(object sender, EventArgs e) 
        {    
              var datasource;
              if (this.RadioButtonList1.SelectedIndex == 1)     
               {         
                  int pageSize = 10; 
                  datasource= GridViewDatasourceCollection.Skip(pageSize * pageNumber).Take(pageSize);    
                  this.GridView1.DataSource= datasource;    
                  this.GridView1.DataBind();     
               }     
               else if (this.RadioButtonList1.SelectedIndex == 2)     
               {         
                  //  the user wants all rows exported, have to turn off paging and rebind                
                  this.GridView1.AllowPaging = datasource;         
                  this.GridView1.DataBind();     
               }     
               GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1); 
           } 

Upvotes: 0

Niladri Biswas
Niladri Biswas

Reputation: 4171

Just a thought

protected void btnExport_Click(object sender, EventArgs e)
{
    var collection = GetTheDataSource(); // Get the source.

    if (this.RadioButtonList1.SelectedIndex == 1)
    {
        //  take first 10 items from the collection     
        collection = collection.Take(10); 
    }
    //set the grid source followed by binding it
    this.GridView1.DataSource = collection;
    this.GridView1.DataBind();

    //Export the grid record to excel
    GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1);
}

Upvotes: 1

Related Questions