Export Datagrid to Excel Save As

G

Guest

I was looking for the easiest way to take a datagrid full of data including
two template columns, and export the contents of the grid into Excel by
giving an "open or save as" box.

I came across the following control which I thought might do it:

http://www.codeproject.com/aspnet/Excel_CSV_export_button.asp

However, I'm not sure if I added the control correctly to my project via
VS2003 or if I am handling the Datagrid to what appears to be required as a
DataView for the Control.

Webform works fine without the control button added.

The error I'm currently getting is:

Unable to generate code for a value of type 'System.Data.DataView'. This
error occurred while trying to generate the property value for Dataview.
Code is as follows:

....
<pnwc:exportbutton id="ExportButton1" runat="server"
FileNameToExport="rptAttachmentFiles.xls" ExportType="Excel"
DataView="DataView1"></pnwc:exportbutton>
....
Protected WithEvents ExportButton1 As PNayak.Web.UI.WebControls.ExportButton
....

My first thought was to try:

Dim DataTable1 As DataTable
Dim DataView1 As DataView
DataTable1 = CType(DataGrid1.DataSource, DataTable)
DataView1 = DataTable1.DefaultView

to convert the Datagrid to a DataView. Is that correct?

Also, doesn't matter if I rem out these DataTable and DataView lines. Same
error.

Thank you in advance.

Rob
 
K

Kevin Yu [MSFT]

Hi Rob,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you're using a 3rd-party control
to generate an Excel file from the DataGrid. If there is any
misunderstanding, please feel free to let me know.

When we're getting the DataSource from the DataGrid, we have to cast it to
the original type when we assign it to the DataGrid.DataSource property. So
if you use a DataTable as the datasource, please cast it to DataTable type.
Since it's a third party control with source code, you can try to trace
into the source code to see what is going wrong.

Actually, it will also be simple for us to export the grid data to an Excel
table with our own code. We can go through each row, write everything we
need to a CSV format text file and save it as an .xls file.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top