?
Solved

DataGrid to Excel not working

Posted on 2007-08-07
11
Medium Priority
?
185 Views
Last Modified: 2010-04-15
I have a web page with a datagrid and a button to export to Excel.  This was working fine, I added the paging ability and now when I export to Excel I only get the data that is currently displayed in the datagrid.  How can I get all of the data to export to Excel?

Thanks
0
Comment
Question by:triplebd69
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 19649384
is it possible for you to convert the object that your datagrid is bound to rather than the datagrid itselft?
0
 
LVL 7

Expert Comment

by:skiltz
ID: 19649753
Sorry I don't know c# but you could try something like this.

GridView1.AllowPaging = False
GridView1.DataBind()
0
 
LVL 4

Accepted Solution

by:
ki_ki earned 1500 total points
ID: 19650040
I had the same problem. I tried the solution above but it didn't work. I had to add a button to turn off paging. I know it's not very 'firendly' but that was the only way a user can send all rows to excel.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:skiltz
ID: 19650095
This certainly works for me - gridview has paging set to true - page 2.  Button click works fine

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        GridView1.AllowPaging = False
        GridView1.DataBind()

        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.xls"
        Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()
        Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
        GridView1.RenderControl(htmlWrite)
        Response.Write(stringWrite.ToString())
        Response.End()

    End Sub
0
 

Author Comment

by:triplebd69
ID: 19653623
skiltz,
you say that paging is set to true put your first line of code you set it to false.

ki ki,

you have any sample code?
0
 

Author Comment

by:triplebd69
ID: 19653643
DotNetThinker:

The datagrid is bound from a dataset, I don't know to use it over the datagrid, any ideas?
0
 

Author Comment

by:triplebd69
ID: 19654396
Well I couldn't figure out a good solution, but did come up with an ugly one.  Put a second dg on the webform that is hidden, and bid the data to it and export from it.  Any other ideas would be nice to see.
0
 
LVL 4

Expert Comment

by:ki_ki
ID: 19656046
triplebd69,

the code to turn off paging is very simple
GridView1.AllowPaging = False

you can even use the same button to turn it on and off
  Me.GridView1.AllowPaging = Not Me.GridView1.AllowPaging
0
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 19656162
I know the question is closed but rather than using a hidden datagrid why not create one dynamically when you need it:

DataGrid dg = new DataGrid()
dg.DataSource = YourDataSet;
dg.DataBind();
//Create excel from this new datagrid.
dg.Dispose();
0
 

Author Comment

by:triplebd69
ID: 19656238
Thanks that would be be a better idea.
0
 

Author Comment

by:triplebd69
ID: 19656330
DotNetThinker,

I tried the dynamic dg, but it throws an error when it gets to the writer.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

621 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