Stupid Sorting!

C

Cliff

in the table:

dt.Columns.Add("Session Duration Seconds",
System.Type.GetType("System.Int32"));

---------------

then the following code to try and sort the table based on that colum

-------------

dv = new DataView(ds.Tables["vpninfo"]);

dv.Sort = "Session Duration Seconds";

foreach (DataRowView dr in dv)
{
Debug.WriteLine(dr["Session Duration Seconds"]);
}

--------------

The output from the debug line

1002
10290
10378
105996
1061542
109
1111
1128591
1136
11483
1164

now I know thats not in order of size.....its in alphabetical order!

how can it get this to sort properly?

Any help appreciated...this one's driving me NUTS!

Cliff Dabbs
 
C

Cliff

Try dt.Columns.Add("Session Duration Seconds",typeof(System.Int32));
instead.

"Cliff" <[email protected]> ha scritto nel messaggio

in the table:
dt.Columns.Add("Session Duration Seconds",
System.Type.GetType("System.Int32"));
---------------

then the following code to try and sort the table based on that colum
-------------

dv = new DataView(ds.Tables["vpninfo"]);
dv.Sort = "Session Duration Seconds";
foreach (DataRowView dr in dv)
{
Debug.WriteLine(dr["Session Duration Seconds"]);
}
--------------

The output from the debug line
1002
10290
10378
105996
1061542
109
1111
1128591
1136
11483
1164

now I know thats not in order of size.....its in alphabetical order!
how can it get this to sort properly?
Any help appreciated...this one's driving me NUTS!
CliffDabbs- Hide quoted text -

- Show quoted text -

Hi,

Thanks for the quick answer, but having tried that, the results are
identical....

Cliff.
 
C

ClayB

If you write out Debug.WriteLine(dr["Session Duration
Seconds"].GetType());, do you see int or string? I suspect string.

What code are you using to place the numbers into the DataTable? The
code below works as expected for me.

DataTable dt = new DataTable("MyTable");

Random r = new Random();
dt.Columns.Add(new DataColumn("Some System.Int32",
typeof(System.Int32)));

for (int i = 0; i < 5; ++i)
{
DataRow dr = dt.NewRow();
dr[0] = r.Next(1000000);
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
dv.Sort = "Some System.Int32";
foreach(DataRowView drv in dv)
Console.WriteLine(drv["Some System.Int32"]);
===============
Clay Burch
Syncfusion, Inc.
 
C

Cliff

If you write out Debug.WriteLine(dr["Session Duration
Seconds"].GetType());, do you see int or string? I suspect string.

What code are you using to place the numbers into the DataTable? The
code below works as expected for me.

DataTable dt = new DataTable("MyTable");

Random r = new Random();
dt.Columns.Add(new DataColumn("Some System.Int32",
typeof(System.Int32)));

for (int i = 0; i < 5; ++i)
{
DataRow dr = dt.NewRow();
dr[0] = r.Next(1000000);
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
dv.Sort = "Some System.Int32";
foreach(DataRowView drv in dv)
Console.WriteLine(drv["Some System.Int32"]);
===============
Clay Burch
Syncfusion, Inc.

Clay,

Thanks for that, yes its coming out as System.String, so my Colum Add
with the Type of System.Int32 obveiously isn't working.

The DataTable is actually being made up by copying from another data
structure in memory, I'm making it to DataTable so that I can use it
in Web stuff easily

the actual code is:

for (int i = 0; i < vbTable.GetLength(0); i++)
{
DataRow dr = dt.Rows.Add();

for (int j = 1; j < vbTable.GetLength(0); j++)
{
try
{
if (vbTable[j] != null)
{
dr[j] = vbTable[j].Value;
}
}
catch (System.NullReferenceException)
{
dr[j] = "";
}
}
}

although the only bit of that that actually does anything is

dr[j] = vbTable[j].Value;

which cycles through each of the colums on each of the rows adding the
STRING value from the source data structure. so yes, here it is a
string thats being passed. vbTable only contains strings.

However, becuase of the way I set the table up, at the point "Session
Duration Seconds" is copied across, I was expecting it to do a
conversion automaticlally...which obveiously isn't happening either!

How can I make that line so it converts to the appropriate type?

Thanks again...

Cliff.
 
C

Cliff

If you write out Debug.WriteLine(dr["Session Duration
Seconds"].GetType());, do you see int or string? I suspect string.
What code are you using to place the numbers into the DataTable? The
code below works as expected for me.
DataTable dt = new DataTable("MyTable");
Random r = new Random();
dt.Columns.Add(new DataColumn("Some System.Int32",
typeof(System.Int32)));
for (int i = 0; i < 5; ++i)
{
DataRow dr = dt.NewRow();
dr[0] = r.Next(1000000);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
dv.Sort = "Some System.Int32";
foreach(DataRowView drv in dv)
Console.WriteLine(drv["Some System.Int32"]);
===============
Clay Burch
Syncfusion, Inc.

Clay,

Thanks for that, yes its coming out as System.String, so my Colum Add
with the Type of System.Int32 obveiously isn't working.

The DataTable is actually being made up by copying from another data
structure in memory, I'm making it to DataTable so that I can use it
in Web stuff easily

the actual code is:

for (int i = 0; i < vbTable.GetLength(0); i++)
{
DataRow dr = dt.Rows.Add();

for (int j = 1; j < vbTable.GetLength(0); j++)
{
try
{
if (vbTable[j] != null)
{
dr[j] = vbTable[j].Value;
}
}
catch (System.NullReferenceException)
{
dr[j] = "";
}
}
}

although the only bit of that that actually does anything is

dr[j] = vbTable[j].Value;

which cycles through each of the colums on each of the rows adding the
STRING value from the source data structure. so yes, here it is a
string thats being passed. vbTable only contains strings.

However, becuase of the way I set the table up, at the point "Session
Duration Seconds" is copied across, I was expecting it to do a
conversion automaticlally...which obveiously isn't happening either!

How can I make that line so it converts to the appropriate type?

Thanks again...

Cliff.- Hide quoted text -

- Show quoted text -


Got it...thanks guys...

I think it was a combination of the number overflowing from 32 to 64
bits and some crap coding on my part....

Thanks again.

Cliff.
 

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

Similar Threads


Top