DataTable sorting, determining type of a column during the sort.

  • Thread starter Thread starter Clinton Pierce
  • Start date Start date
C

Clinton Pierce

I've filled a DataTable with columns that have custom type (a class that I'm
using to keep track of other things, not just a value). When the .Select
method goes to sort this column, how do I let .Net know what value I want
used for the sort?

Sometimes it's going to be sorted as a decimal and other times a string. I
don't really know which until runtime.

Thank you.
 
you specify select statement like this..

Table.Select("Col1 > 10","Col2 asc");

based on your Col2 datatype, its going to sort. if your Col2 is of string
type, it will do string comparision to sort. and same for int or any other
datatype.

hth,
Av.
 
You missed the point.

The table doesn't contain strings, decimals, integers, or anything like
that. It contains my own objects. The columns were defined something like
this:

System.Type mot = new MyObject.GetType();
DataColumn dc = new DataColumn("somename", mot);

When I sort on "somename" it's arbitrarily calling MyObject's ToString()
method to get the value for sorting. This is wrong. I know it's being
stored as the object because I can pull it out again later and use it just
fine like this:

MyObject foo = ((MyObject)dataSet.Table[tablename].Rows[0]["somename"]);

For *sorting* purposes at runtime I can look at an individual Object of type
MyObject and get a sortable-representation of it (string, decimal, etc..).
And, by design, everything in a column of a table will wind up with a
similar sortable representation.

What I expected to happen was that for Objects in a table that
DataTable.Select() didn't know about, it would look for an ICompare
interface or something on an object. It doesn't appear to, it's just
calling ToString().
 
Back
Top