473,394 Members | 2,071 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,394 software developers and data experts.

Sorting DataTable

WB
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 instance, if you try to run
this code:

DataTable userTable = new DataTable();

userTable.Columns.Add("userID", Type.GetType("System.Int32"));
userTable.Columns.Add("username", Type.GetType("System.String"));

DataRow userRow = userTable.NewRow();
userRow["userID"] = 1;
userRow["username"] = "Peter";
userTable.Rows.Add(userRow);
userRow = userTable.NewRow();
userRow["userID"] = 2;
userRow["username"] = "Paul";
userTable.Rows.Add(userRow);
userRow = userTable.NewRow();
userRow["userID"] = 3;
userRow["username"] = "Mary";
userTable.Rows.Add(userRow);

Trace.Warn("*********** Before sorting ***********");
foreach (DataRow userInfo in userTable.DefaultView.Table.Rows)
{
Trace.Warn("userID = " + userInfo["userID"].ToString(), "username = " +
userInfo["username"].ToString());
}

// Perform sorting
userTable.DefaultView.Sort = "username";

Trace.Warn("*********** After sorting ***********");
foreach (DataRow userInfo in userTable.DefaultView.Table.Rows)
{
Trace.Warn("userID = " + userInfo["userID"].ToString(), "username = " +
userInfo["username"].ToString());
}

The trace before and after sorting are the same. If I bind it to a data
control, the result of the sorting will show. But why not when I iterate
through the rows? What am I missing here?
WB.
Feb 20 '06 #1
3 1855
EJD
WB,
Try using this instead and see if there is a difference:
foreach (DataRowView userInfo in userTable.DefaultView)
{
Trace.Warn("userID = " + userInfo["userID"].ToString(),
"username = " +
userInfo["username"].ToString());

}
By applying the sort to the DefaultView you're asking for what you
want, but when iterating through the userTable.DefaultView.Table.Rows
it's giving you the table in its original state. HTH.
Eric

Feb 21 '06 #2
Hi,
I hope sorting occurs only at the time of binding to a datagrid and not in
the assigment statement. If you want to sort and use it for looping try using
the following code..

Trace.Warn("*********** After sorting ***********");
DataView objSorted = userTable.DefaultView;
System.Collections.IEnumerator enum = objSorted.GetEnumerator();

DataRowView dr;
while(enum.MoveNext())
{
dr = (DataRowView)enum.Current;
Trace.Warn("userID = " + dr.Row["userID"].ToString(), "username = " +
dr.Row["username"].ToString());
}

Hope this would help..
"WB" wrote:
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 instance, if you try to run
this code:

DataTable userTable = new DataTable();

userTable.Columns.Add("userID", Type.GetType("System.Int32"));
userTable.Columns.Add("username", Type.GetType("System.String"));

DataRow userRow = userTable.NewRow();
userRow["userID"] = 1;
userRow["username"] = "Peter";
userTable.Rows.Add(userRow);
userRow = userTable.NewRow();
userRow["userID"] = 2;
userRow["username"] = "Paul";
userTable.Rows.Add(userRow);
userRow = userTable.NewRow();
userRow["userID"] = 3;
userRow["username"] = "Mary";
userTable.Rows.Add(userRow);

Trace.Warn("*********** Before sorting ***********");
foreach (DataRow userInfo in userTable.DefaultView.Table.Rows)
{
Trace.Warn("userID = " + userInfo["userID"].ToString(), "username = " +
userInfo["username"].ToString());
}

// Perform sorting
userTable.DefaultView.Sort = "username";

Trace.Warn("*********** After sorting ***********");
foreach (DataRow userInfo in userTable.DefaultView.Table.Rows)
{
Trace.Warn("userID = " + userInfo["userID"].ToString(), "username = " +
userInfo["username"].ToString());
}

The trace before and after sorting are the same. If I bind it to a data
control, the result of the sorting will show. But why not when I iterate
through the rows? What am I missing here?
WB.

Feb 21 '06 #3
WB,

Normally there is no reason to sort a datatable (the only reason can be is
that you use it as XML file externally wrapped in a Dataset).

However if you use version 2005 then there is now a new method to make a
copy from a datatable using the dataview.

Dataview.ToTable(with 4 overloaded possibilities)

If you use 2003

http://www.vb-tips.com/default.aspx?...7-d6ad9bebf57f

I hope this helps,

Cor
Feb 21 '06 #4

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....
6
by: David P. Donahue | last post by:
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.