Solved

Help exporting DatAGridView to Excel

Posted on 2012-04-11
5
861 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 69

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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now