Exporting to Excel and Column Order

G

Guest

Hi,

I have a DataGrid, whose sourceI am exporting to Excel. This works fine
except for the Column ordering. My datasource is not a datatable, with a
typical
SELECT statement where I can select the column orders. Instead the datasource
is a class which implements IList, containing a collection of my data. My
problem
again is that I don't know how to control the order of the columns that are
exported.

Any Ideas?

Thanks
 
S

S. Justin Gengo

Opa,

This isn't a solution but may lead you in the correct direction. You will
have to find a way to sort the columns. You may need to make a customized
sort. Which can be added on to your class. But here's a simple default
method:

public IList Sort(IList list)
{
((ArrayList)list).Sort();
return list;
}

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
G

Guest

Jusin, Thanks for the reply.
Perhaps you misunderstand the question.
I am not trying to sort the rows by a specific column,
but rather I am trying to manipulate the ordering or
or columns. Get it?

Any Ideas?
 
A

addup

Opa said:
Jusin, Thanks for the reply.
Perhaps you misunderstand the question.
I am not trying to sort the rows by a specific column,
but rather I am trying to manipulate the ordering or
or columns. Get it?

Any Ideas?

I'm not sure i follow either, but...
I persume your DataGrid is set to autogenerate columns.
Dont.
Define bound (or whatever) columns, in the order that you want.

hope this helps
-- a --
 
G

Guest

I tried setting AutoGenerateColumns to false and then adding a single column,
but this does not work. Funny thing is that it seems to ignore the
AutoGenerateColumns=false, and yet it generate all the columns anyway.
Any Ideas?

Here is my code:

public static void Convert(IList list, HttpResponse response, string fileName)
{
//first let's clean up the response.object
response.Clear();
response.Charset = string.Empty;

//This will make the browser interpret the output as an Excel file
response.AddHeader("Content-Disposition", "attachment; filename=" +
fileName + ".xls");

//set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
//create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

//instantiate a datagrid and set the datagrid datasource to the dataset
passed in
DataGrid dg = new DataGrid();
dg.DataSource = list;
dg.DataBind();

dg.AutoGenerateColumns = false;

//DataGridColumnCollection colCol = new DataGridColumnCollection(gd,
cols);
BoundColumn col = new BoundColumn();
col.DataField = "WorkOrderNumber";
col.HeaderText= "Work Order #";
col.ItemStyle.ForeColor = System.Drawing.Color.Blue;

dg.Columns.Add(col);

//tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite);

response.Write("<B>" + fileName + "</B><BR/>");
response.Write("<B>" + DateTime.Now.ToString("F") + "</B><BR/><BR/>");

//all that's left is to output the html
response.Write(stringWrite.ToString());
response.End();
}
 
A

addup

Opa said:
I tried setting AutoGenerateColumns to false and then adding a single column,
but this does not work. Funny thing is that it seems to ignore the
AutoGenerateColumns=false, and yet it generate all the columns anyway.
Any Ideas?

Here is my code:

public static void Convert(IList list, HttpResponse response, string fileName)
{
//first let's clean up the response.object
response.Clear();
response.Charset = string.Empty;

//This will make the browser interpret the output as an Excel file
response.AddHeader("Content-Disposition", "attachment; filename=" +
fileName + ".xls");

//set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
//create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

//instantiate a datagrid and set the datagrid datasource to the dataset
passed in
DataGrid dg = new DataGrid();
dg.DataSource = list;
dg.DataBind();

dg.AutoGenerateColumns = false;

//DataGridColumnCollection colCol = new DataGridColumnCollection(gd,
cols);
BoundColumn col = new BoundColumn();
col.DataField = "WorkOrderNumber";
col.HeaderText= "Work Order #";
col.ItemStyle.ForeColor = System.Drawing.Color.Blue;

dg.Columns.Add(col);

//tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite);

response.Write("<B>" + fileName + "</B><BR/>");
response.Write("<B>" + DateTime.Now.ToString("F") + "</B><BR/><BR/>");

//all that's left is to output the html
response.Write(stringWrite.ToString());
response.End();
}


That's because you're doing your dg.DataBind first.
Do your databind *after* setting up the columns

i.e. just before the dg.RenderControl

-- a --
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top