DataTable Sort and Select not working....

J

Johan Pingree

I am building a C# app. below find the test code I have which makes for the short story:
The Legacy object has no method to which to search or sort it's data so I am creating
a DataTable and populating it so that I can take advantage of it's features.

//I create a DataTable in my class and add the columns I need...
DataTable dt = new DataTable("LegacyData");
dt.Columns.Add("TS", typeof(DataTime));
dt.Columns.Add("FName", typeof(string));
dt.Columns.Add....blah blah -> I create about a dozen columns.
dt.DefaultView.Sort = "TS DESC";

//I then populate the DataTable
public void AddDataToTable(LegacyObject lo)
{
DataRow dr;
for (int i = 0; i < lo.Count; i++)
{
dr = dt.NewRow();
dr["TS"] = DateTime.Parse(lo.TS);
dr["FName"] = lo.FName;
//>>>> and so forth.....
dt.Rows.Add(dr);
}
}

//Now I want to organize the data for display

dt.Select("TS > '4/1/04'", "TS DESC")
foreach(DataRow dr in dt.Rows)
{
Console.Write("Person: " + dr["FName"].ToString() + " " + dr["LName"].ToString());
Console.Write(" Dated: " + dr["TS"].ToString());
}

All the data prints to the console, however the TS column is NOT sorted.

What went wrong!!
 
M

Miha Markic [MVP C#]

Hi Johan,

DataTable.Select returns an array of DataRow instances and does *not* sort
datatable rows.
Use something like:
foreach (DataRow dr in dt.Select(....))

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

I am building a C# app. below find the test code I have which makes for the
short story:
The Legacy object has no method to which to search or sort it's data so I am
creating
a DataTable and populating it so that I can take advantage of it's features.

//I create a DataTable in my class and add the columns I need...
DataTable dt = new DataTable("LegacyData");
dt.Columns.Add("TS", typeof(DataTime));
dt.Columns.Add("FName", typeof(string));
dt.Columns.Add....blah blah -> I create about a dozen columns.
dt.DefaultView.Sort = "TS DESC";

//I then populate the DataTable
public void AddDataToTable(LegacyObject lo)
{
DataRow dr;
for (int i = 0; i < lo.Count; i++)
{
dr = dt.NewRow();
dr["TS"] = DateTime.Parse(lo.TS);
dr["FName"] = lo.FName;
//>>>> and so forth.....
dt.Rows.Add(dr);
}
}

//Now I want to organize the data for display

dt.Select("TS > '4/1/04'", "TS DESC")
foreach(DataRow dr in dt.Rows)
{
Console.Write("Person: " + dr["FName"].ToString() + " " +
dr["LName"].ToString());
Console.Write(" Dated: " + dr["TS"].ToString());
}

All the data prints to the console, however the TS column is NOT sorted.

What went wrong!!
 
M

Marina

Select returns an array of datarows - this array is what is sorted. So you need to loop through this array, not through the rows collection of the datatable - which remains unchanged.

This is fully documented in the MS documentation.
I am building a C# app. below find the test code I have which makes for the short story:
The Legacy object has no method to which to search or sort it's data so I am creating
a DataTable and populating it so that I can take advantage of it's features.

//I create a DataTable in my class and add the columns I need...
DataTable dt = new DataTable("LegacyData");
dt.Columns.Add("TS", typeof(DataTime));
dt.Columns.Add("FName", typeof(string));
dt.Columns.Add....blah blah -> I create about a dozen columns.
dt.DefaultView.Sort = "TS DESC";

//I then populate the DataTable
public void AddDataToTable(LegacyObject lo)
{
DataRow dr;
for (int i = 0; i < lo.Count; i++)
{
dr = dt.NewRow();
dr["TS"] = DateTime.Parse(lo.TS);
dr["FName"] = lo.FName;
//>>>> and so forth.....
dt.Rows.Add(dr);
}
}

//Now I want to organize the data for display

dt.Select("TS > '4/1/04'", "TS DESC")
foreach(DataRow dr in dt.Rows)
{
Console.Write("Person: " + dr["FName"].ToString() + " " + dr["LName"].ToString());
Console.Write(" Dated: " + dr["TS"].ToString());
}

All the data prints to the console, however the TS column is NOT sorted.

What went wrong!!
 

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