Hi, can anyone tell me why the following .sort command (in bold) does not actually do anything? I'm stumped. I can take out the command and the table is sorted the same way as when I leave the command in. From what I've read, this should work, but maybe
I'm missing something?
DataSet holdTable =
new DataSet();
DataTable dt = holdTable.Tables.Add();
dt.Columns.Add("Path",
typeof(string));
dt.Columns.Add("Visitors",
typeof(int));
dt.Columns.Add("Hits",
typeof(int));
dt.Columns.Add("SecsPerPath",
typeof(double));
dt.Columns.Add("SecsPerUser",
typeof(double));
foreach (KeyValuePair<string,
int> kvp in allPaths)
{
DataRow row = dt.NewRow();
row["Path"] = kvp.Key;
row["Visitors"] = kvp.Value;
//essentially a distinct on each session_id
Sort is not working because you have INT, STRING and DOUBLE data type and no ICOMPARER interface or SQL statement with ORDERBY, sort require equality of types. In .NET you use the ICOMPARER interface and ORDERBY in SQL. Hope this helps.
heck0045
Member
6 Points
6 Posts
DataView.Sort is not working?
Jul 07, 2006 02:41 PM|LINK
Hi, can anyone tell me why the following .sort command (in bold) does not actually do anything? I'm stumped. I can take out the command and the table is sorted the same way as when I leave the command in. From what I've read, this should work, but maybe I'm missing something?
DataSet holdTable = new DataSet();DataTable dt = holdTable.Tables.Add();
dt.Columns.Add("Path", typeof(string));
dt.Columns.Add("Visitors", typeof(int));
dt.Columns.Add("Hits", typeof(int));
dt.Columns.Add("SecsPerPath", typeof(double));
dt.Columns.Add("SecsPerUser", typeof(double));
foreach (KeyValuePair<string, int> kvp in allPaths)
{
DataRow row = dt.NewRow();
row["Path"] = kvp.Key;
row["Visitors"] = kvp.Value; //essentially a distinct on each session_id
row[
"Hits"] = totalPagesInPath[kvp.Key];row[
"SecsPerPath"] = totalTimeOnPath[kvp.Key] / Convert.ToDouble(kvp.Value);row[
"SecsPerUser"] = totalTimeOnPath[kvp.Key] / Convert.ToDouble(totalPagesInPath[kvp.Key]); //total time on entire path divided by the number of pagesdt.Rows.Add(row);
}
//sort dataset by visitors desc DataView rView = holdTable.Tables[0].DefaultView;rView.Sort =
"[Visitors] DESC"; //create specialized table string spTable = "<table width=100% cellspacing=0 cellpadding=2 border=1 rules=none width=350><tr><td colspan=4 height=22 bgcolor='#0F2B5B'><b><font color='white'>Navigation Patterns</font></b></td></tr>"; foreach (DataRow nRow in rView.Table.Rows){
..the rView.Table.Rows is NOT sorted?
Caddre
All-Star
26581 Points
5308 Posts
Re: DataView.Sort is not working?
Jul 07, 2006 06:17 PM|LINK
Gift Peddie
fuelpagan
Member
12 Points
1 Post
Re: DataView.Sort is not working?
Feb 20, 2013 10:04 PM|LINK
You're long past this problem, but for those who find this looking for help like myself...
I ran into this issue and discovered the problem is in the loop reading the data.
foreach (DataRow nRow in rView.Table.Rows)
<div>This looks back at the original table ignoring the sort you just assigned.</div>It should read:
foreach (DataRowView nRow in rView)
This will now loop through the sorted data like you expected the program to.