Solved

Help exporting DatAGridView to Excel

Posted on 2012-04-11
5
869 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Send SMS from vb.net desktop app 30 46
AJAX Create / Update Not working MVC 5 59
Intermittent OleDbConnection Error 20 46
Adding Rows to Grid 4 28
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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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