By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Export Datagrid to Excel

P: n/a
Hi,

I have to export data inside the datagrid into an excel worksheet. I have
to do this on button click event. I am trying to do like this.

First get the DataTable from the datagrid and iterate through each row to
get the data. I am not able to get the DataTable from the datagrid. How could
I do that? I tried like this but it is not working

Dim dt as DataTable = CType(datagrid1.DataSource, DataTable)

I have also tried this

Dim t as Table = datagrid1.Controls(0). This one is giving the data in the
form of a table. but the problem is, If I have a datagrid that contains
LinkButtons and If I need to get the text of that LinkButtons, it is not
getting them. Is there a way to get the full data even the text on the
controls so that I could export it to excel?

Thnx!
Jan 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Sridhar,

A while ago i came across this site
http://www.c-sharpcorner.com/Code/20...ridToExcel.asp .
This might help.
Good luck.
"Sridhar" wrote:
Hi,

I have to export data inside the datagrid into an excel worksheet. I have
to do this on button click event. I am trying to do like this.

First get the DataTable from the datagrid and iterate through each row to
get the data. I am not able to get the DataTable from the datagrid. How could
I do that? I tried like this but it is not working

Dim dt as DataTable = CType(datagrid1.DataSource, DataTable)

I have also tried this

Dim t as Table = datagrid1.Controls(0). This one is giving the data in the
form of a table. but the problem is, If I have a datagrid that contains
LinkButtons and If I need to get the text of that LinkButtons, it is not
getting them. Is there a way to get the full data even the text on the
controls so that I could export it to excel?

Thnx!

Jan 3 '06 #2

P: n/a
May I also recommend the following for VB programmers, I use this one
to export my grid to Excel. Works like a charm.

http://dotnetjunkies.com/WebLog/stef.../01/27337.aspx

Jan 4 '06 #3

P: n/a
Hi Alex,

Thanks for the reply. It is working fine for some pages. But for some
pages it is exporting the whole page not just the datagrid. I am thinking it
is due to the HttpContext.Current property. Is there a way to clear all the
data and just export the datagrid?

Thanks,
Sridhar.

"Alex A." wrote:
May I also recommend the following for VB programmers, I use this one
to export my grid to Excel. Works like a charm.

http://dotnetjunkies.com/WebLog/stef.../01/27337.aspx

Jan 4 '06 #4

P: n/a
I see a lot of the same code out there to export data from an ASP.NET
DataGrid to Excel. All the solutions are pretty similar. One thing I have
noticed with the solution(s) provided is that if a user opens the file,
rather than saving it, the Excel file will open in the user's browser window
and disable the Back button. How can you allow a user to open the Excel file
(instead of saving it off first) and be able to use the Back button to get
back to the site?

Thanks!

Brian

"Sridhar" wrote:
Hi Alex,

Thanks for the reply. It is working fine for some pages. But for some
pages it is exporting the whole page not just the datagrid. I am thinking it
is due to the HttpContext.Current property. Is there a way to clear all the
data and just export the datagrid?

Thanks,
Sridhar.

"Alex A." wrote:
May I also recommend the following for VB programmers, I use this one
to export my grid to Excel. Works like a charm.

http://dotnetjunkies.com/WebLog/stef.../01/27337.aspx

Feb 24 '06 #5

P: n/a
I have the same problem as you, Have you found a solution?
--
Kenny M.
"Sridhar" wrote:
Hi Alex,

Thanks for the reply. It is working fine for some pages. But for some
pages it is exporting the whole page not just the datagrid. I am thinking it
is due to the HttpContext.Current property. Is there a way to clear all the
data and just export the datagrid?

Thanks,
Sridhar.

"Alex A." wrote:
May I also recommend the following for VB programmers, I use this one
to export my grid to Excel. Works like a charm.

http://dotnetjunkies.com/WebLog/stef.../01/27337.aspx

Mar 16 '06 #6

P: n/a
Nope...Never found a solution to it...be sure to post back here if you find
one!!

Brian

"Kenny M." wrote:
I have the same problem as you, Have you found a solution?
--
Kenny M.
"Sridhar" wrote:
Hi Alex,

Thanks for the reply. It is working fine for some pages. But for some
pages it is exporting the whole page not just the datagrid. I am thinking it
is due to the HttpContext.Current property. Is there a way to clear all the
data and just export the datagrid?

Thanks,
Sridhar.

"Alex A." wrote:
May I also recommend the following for VB programmers, I use this one
to export my grid to Excel. Works like a charm.

http://dotnetjunkies.com/WebLog/stef.../01/27337.aspx

Mar 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.