user1342164
user1342164

Reputation: 1454

How to export a datasource's selected data to excel?

I am using the code below which will export the gridview to an excel sheet. Problem is it will only export the records/columns on the page (10 of them) because my gridview setting is set to only show 10 rows per page and only certain columns. Is there a way I can export all records and columns from a datasource to excel?

 Protected Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
    Response.Clear()
    Response.Buffer = True
    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""
    Me.EnableViewState = False
    Dim oStringWriter As New System.IO.StringWriter
    Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

    GridView1.RenderControl(oHtmlTextWriter)

    Response.Write(oStringWriter.ToString())
    Response.[End]()
End Sub

Upvotes: 0

Views: 770

Answers (1)

Garrison Neely
Garrison Neely

Reputation: 3289

When I instituted database-level paging, I had to create a separate "Export" page that would render all of the data without paging into a separate GridView. Then I called the function you have in your question to render it. I suggest you do it that way.

Upvotes: 1

Related Questions