Solved

Help exporting DatAGridView to Excel

Posted on 2012-04-11
5
858 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yea I know for the 'state' I just copied paste your line :)
0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

11 Experts available now in Live!

Get 1:1 Help Now