G
Guest
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.
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.