By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,651 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,651 IT Pros & Developers. It's quick & easy.

Sorting A DataTable

P: n/a
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
dd******@ccs.neu.edu
http://www.cyber0ne.com
Jun 23 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:OC**************@TK2MSFTNGP04.phx.gbl...
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
dd******@ccs.neu.edu
http://www.cyber0ne.com

Jun 23 '06 #2

P: n/a
> 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
dd******@ccs.neu.edu
http://www.cyber0ne.com
Jun 23 '06 #3

P: n/a
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

David P. Donahue wrote:
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
dd******@ccs.neu.edu
http://www.cyber0ne.com


Jun 23 '06 #4

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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
dd******@ccs.neu.edu
http://www.cyber0ne.com

Jun 23 '06 #5

P: n/a
> 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
dd******@ccs.neu.edu
http://www.cyber0ne.com
Jun 23 '06 #6

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:er**************@TK2MSFTNGP05.phx.gbl...
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
dd******@ccs.neu.edu
http://www.cyber0ne.com

Jun 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.