473,382 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Sorting A DataTable

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
6 3328
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
> 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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
1
by: Jeremy | last post by:
I want my gird to sort only the items on the current page when I click on a column header. I wrote a little test app, but when I sort it pulls in items from other pages and places them on the current...
4
by: Richard | last post by:
When i try sorting in the database, it sorts the numbers: 0 1 102 2 304 305 4 etc....
3
by: WB | last post by:
Hi, I have a DataTable, which I'd like to sort before using it for other operation. However, I notice that even after I call the .DefaultView.Sort = "username", the view is still not sorted. For...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
4
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have a vb.net 2.0 app that is loading a GridView with a DataSource that is returned from a function. The definitions in the function are: Dim ReportDS As DataSet = New DataSet Dim...
3
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a question that maybe somebody can help me out. I have a gridview that is bound to a sqltable, and I have created two template columns. I am having problems getting the sorting to work....
0
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
1
by: castron | last post by:
Hello All, I have a grid view that allows sorting, paging, editing, etc. Under On Load event, if I check: if(!IsPostBack){ DisplayData(); }, the Edit portion works fine. However, the Sorting...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.