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!!
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!!