Solved

Help exporting DatAGridView to Excel

Posted on 2012-04-11
5
865 Views
Last Modified: 2012-04-12
Hello EE,

I have a DataGridview I would like to export to excel. I know how to do it with for next...
my problem is the column order in the excel needs to be a little bit different.

lets say Columns in the dataGrid are :

TotalCredit      01Credit     02Credit   03Credit    Code      Desc     Total Debit


I would like in excel the order to be:

Code    Desc    TotalCredit   Total Debit    01Credit     02Credit   03Credit



How can I achieve this with code example ?

thanks
0
Comment
Question by:PhilippeRenaud
  • 2
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 37833750
you can use something like http://www.emoreau.com/Entries/Articles/2009/04/Using-LINQ-and-XML-Literals-to-transform-a-DataTable-into-a-HTML-table.aspx to produce your output. by changing the order of the fields in the SELECT clause, you will get exactly what you want.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37833798
So I would do something like :

From state In DataGridview1.Datasource.AsEnumerable _

...

and then select the order I want in linq ?

then after a for next with 'state' to create my excel ?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37833812
cast your datasource as dataset or datatable (if it is what they are originally)

state can be replace with whatever you want. in my demo, I used state because I am looping a states table
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37833820
yea I know for the 'state' I just copied paste your line :)
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37833849
get the datasource of your gridview, create a new datatable with columns as per your needs.
Fill this new datatable with data from your gridview datasource.

Create a new gridview and assign this new datatable as its datasource.
Then save this gridview to excel, here is a sample code:-
DataTable dtOriginal=gridView1.DataSource;
DataTable dtNew=new DataTable();
//add columns to datatable as required and copy the data

DataGrid rv=new DataGrid();
rv.DataSource=dtNew;

Open in new window

here are few ways to do this:-
http://www.codeproject.com/Articles/164582/8-Solutions-to-Export-Data-to-Excel-for-ASP-NET
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 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