Sorting A DataTable

D

David P. Donahue

I've been looking around for ways to sort the rows in a DataTable, and
everything seems to point to just changing the Sort property on that
DataTable's DefaultView property. That's all well and good for viewing
it sorted, but I need it to actually _be_ sorted, and testing seems to
show that the view doesn't do the trick. If I iterate through the rows
in my code and look at the sorted column in each step, it's not sorted.

Now, I can sort in my query easily enough before populating the
DataTable, but in this particular case this DataTable gets built through
multiple identical-result-schema queries which execute, perform some
other logic, and then append their data to the main DataTable. Thus,
sorting in each query would still result in the final DataTable being
unsorted.

Is there an easy way to actually sort the rows in the DataTable? Should
the Sort property on the DefaultView be doing the trick if used
properly? Or am I going to have to write a sorting algorithm to do this
(not difficult, but performance is key and I'm hoping that the language
has something built-in to out-perform whatever I write)?


Regards,
David P. Donahue
(e-mail address removed)
http://www.cyber0ne.com
 
N

Nicholas Paldino [.NET/C# MVP]

David,

I would argue that you are using the DataTable incorrectly. The
DataTable isn't supposed to be aware of sort order and things of that
nature.

Instead, why not use the DataView as you would the DataTable? The
DataView will iterate just like the DataTable (except in sorted order) and
you have access to the underlying rows as well as the underlying table.

Also, the DefaultView property is used for when binding to a DataTable
(it is what is used when a view is not).

However, I would suggest that you look at the DataTable as nothing more
than a store for data. Let the DataView class handle the filtering and
sorting, and drop down to the row access if you need it (although
DataRowView does expose the values as the row does as well).

Hope this helps.
 
D

David P. Donahue

I would argue that you are using the DataTable incorrectly.

Quite possibly.
Instead, why not use the DataView as you would the DataTable? The
DataView will iterate just like the DataTable (except in sorted order) and
you have access to the underlying rows as well as the underlying table.

Perhaps I'm just not seeing it properly, then. This could be entirely
the fault of my IDE (I'm accustomed to Visual Studio, but at work we're
now doing C# programming with Borland Delphi 2005).

Ok, I have a DataTable. I create a new DataView, point it to that table
and set the Sort property to the name of the column I want sorted. My
for loop can use the DataView's Count property as the terminating
condition for iterating. Now, for each iteration, how do I access
fields in that row? Say, for example, my loop iterator is "i" and I
want to store one of the items in row i into a string. What would that
line of code look like?


Regards,
David P. Donahue
(e-mail address removed)
http://www.cyber0ne.com
 
J

Jim

David-

Changing the Sort property on the Datatable will not sort the rows for
you. You can use a DataView to do the sort:

// you have a DataTable called table
DataView view = new DataView(table);
view.Sort = "LAST_NAME";

DataTable sorted = view.ToTable();

That should do it. If you need more sorting options I think you can
get carazy with the Sort property of the DataView and do things like
"LAST_NAME, FIRST_NAME, DESC"

Jim Suruda
 
N

Nicholas Paldino [.NET/C# MVP]

David,

That's simple. Say you have your DataView:

DataView dv = <doesn't matter, assume dv is set up properly>;

You can loop through (and not use the Count property either) like so:

foreach (DataRowView dr in dv)
{
// Use dr here like you would DataRow. If you need the Row itself for
some reason
// use the Row property on dr.
// So, to get a value, you could just do
int intValue = (int) dr["intColumn"];
}

And so on.
 
D

David P. Donahue

foreach (DataRowView dr in dv)
{
// Use dr here like you would DataRow. If you need the Row itself for
some reason
// use the Row property on dr.
// So, to get a value, you could just do
int intValue = (int) dr["intColumn"];
}

Ah, now I see. A DataView is an array of DataRowViews, which are arrays
of the values I'd need to read. Should I want to write to the
DataTable, each DataRowView has a Row property that is a direct
reference to its corresponding row in the DataTable.

Sweet. Thanks!


Regards,
David P. Donahue
(e-mail address removed)
http://www.cyber0ne.com
 
N

Nicholas Paldino [.NET/C# MVP]

David,

Actually, if you want to write the value to the row, you can do so
through the DataRowView returned to you. The indexer (which returns the
value) has a get and set on it.

Enjoy.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

David P. Donahue said:
foreach (DataRowView dr in dv)
{
// Use dr here like you would DataRow. If you need the Row itself
for some reason
// use the Row property on dr.
// So, to get a value, you could just do
int intValue = (int) dr["intColumn"];
}

Ah, now I see. A DataView is an array of DataRowViews, which are arrays
of the values I'd need to read. Should I want to write to the DataTable,
each DataRowView has a Row property that is a direct reference to its
corresponding row in the DataTable.

Sweet. Thanks!


Regards,
David P. Donahue
(e-mail address removed)
http://www.cyber0ne.com
 

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