In memory sort a DataTable in DataSet

H

Hardy Wang

Hi,
I have a DataSet, returned by a stored procudure, which has only on
DataTable. At this step there is no sort in DataTable. Is there is a way I
can do to sort DataRows in this DataTable (not from select statement of sql
query) in memory?
Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Hardy,

Absolutely. Create a new instance of a DataView class, and then pass
the DataTable you want to sort to the DataView. Once you do that, you can
set the Sort property to the fields that you want to sort the DataTable by.

Then, you would access the rows through the data view, not through the
table, and you will see them in order.

Hope this helps.
 
M

Miha Markic [MVP C#]

Hi Hardy,

Just an addition to Nicholas' post.
You could use DataTable.DefaultView (as a DataView instance Nicholas
mentioned) which is also used as a datasource when the DataTable is bound.
 
C

Cor Ligthert

Hardy,

When you want see a datatable in a special sequence you can use the dataview
to view the datatable in sorted order.

When you want to sort the datatable, you can clone that table and than loop
thru the dataview row by row and add the rows to that new datatable, which
can in a generic way because the rows and the items from the dataview are
equal on the datatable (when you do not set a dataview.rowfilter).

I hope this helps?

Cor

"Hardy Wang"
 
M

Miha Markic [MVP C#]

When you want to sort the datatable, you can clone that table and than
loop
thru the dataview row by row and add the rows to that new datatable, which
can in a generic way because the rows and the items from the dataview are
equal on the datatable (when you do not set a dataview.rowfilter).

Hi Cor,

A note:
Although this is technically correct, there is no much sense in sorting the
datatable (not that I am implying that you think otherwise).
Just my opinion :)
 
C

Cor Ligthert

Miha,
Hi Cor,

A note:
Although this is technically correct, there is no much sense in sorting the
datatable (not that I am implying that you think otherwise).
Just my opinion :)
I was expecting you would write this.

In common use there is no reason, however there is at least one, when you
want to export a dataset to a XML file on disk or whatever and use that in a
loadXML(doc) or whatever to process even in not dotNet environments
sequentially.

Can we make it 1:1 or can you make it 2:0

(Although I have the same meaning as you of course, it is for every day
learning so loosing is winning)

:)

Cor
 
M

Miha Markic [MVP C#]

Cor Ligthert said:
Miha,
I was expecting you would write this.

In common use there is no reason, however there is at least one, when you
want to export a dataset to a XML file on disk or whatever and use that in
a
loadXML(doc) or whatever to process even in not dotNet environments
sequentially.

I would preffer using XPath sorting for this purpose. :)
 

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

Top