In memory sort a DataTable in DataSet

  • Thread starter Thread starter Hardy Wang
  • Start date Start date
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
 
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.
 
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.
 
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"
 
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 :-)
 
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
 
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. :-)
 
Back
Top