Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I have a gridview that is data-bound from my SQL database. The data is too much to view all at once on the Web screen so I allowed it to page the gridview. Before I did this I have a button that can export the gridview to excel format and I can open it up and see all of my data. Now I have the paging enabled and I export the gridview it only exports that first page of the gridview and not the whole thing. Is there a way I can export the gridview with all the pages without changing the gridview back to its original state?

C#
protected void ButtonExcel_Click(object sender, EventArgs e)
{
    <pre>Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=FINSubmitted.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    System.IO.StringWriter sw = new System.IO.StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView2.RenderControl(hw);
    string style = @"<style> .textmode { } </style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}


What I have tried:

I have tried to changed the Gridview.Allowpaging = false;
Posted
Updated 6-Jul-17 4:48am
v2
Comments
F-ES Sitecore 22-Jun-17 11:55am    
When you're calling BindGrid in your ButtonExcel_Click function you need to disable the paging so that all rows are rendered. I'd probably add a parameter to the function that lets you control when it needs to implement paging or not

private void BindGrid(bool enablePaging)
{
}

Now when you call the function you pass true or false as needed. How you disable paging we can't tell you as you haven't explained how you've enabled it.
Computer Wiz99 22-Jun-17 14:00pm    
Thanks. This is how I am enabling paging.
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
                        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
                        DataSourceID="SqlDataSourceTrueSubmitted" ForeColor="#333333" GridLines="None" 
                        Height="224px" style="text-align: center">
F-ES Sitecore 23-Jun-17 4:25am    
So use

GridView2.AllowPaging = false;

to disable it when you want to download it to the client
Computer Wiz99 23-Jun-17 12:07pm    
I have done that but it still exports to excel with the paging. Not the full database is exported.

try creating a new Gridview object
protected void ButtonExcel_Click(object sender, EventArgs e)
       {
           Response.Clear();
           Response.Buffer = true;
           Response.AddHeader("content-disposition", "attachment;filename=FTESubmitted.xls");
           Response.Charset = "";
           Response.ContentType = "application/vnd.ms-excel";
           System.IO.StringWriter sw = new System.IO.StringWriter();
           HtmlTextWriter hw = new HtmlTextWriter(sw);
           GridView GridviewExcel = new GridView();
           GridviewExcel.AllowPaging = false;
           GridviewExcel.DataSource = yourSource;
           GridviewExcel.RenderControl(hw);
           string style = @"<style> .textmode { } </style>";
           Response.Write(style);
           Response.Output.Write(sw.ToString());
           Response.Flush();
           Response.End();
       }
 
Share this answer
 
C#
protected void ButtonExcel_Click(object sender, EventArgs e)
        {

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=FINSubmitted.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView2.AllowPaging = false;
            GridView2.DataBind();
            GridView2.RenderControl(hw);
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900