views:

193

answers:

3

I have a datatable something like this:

     |  Col1  |  Col6  |  Col3  |  Col43 |  Col0  |
---------------------------------------------------
RowA |   1    |    6   |   54   |    4   |   123  |

As you see, the Cols are not sorted by their numbers. That is what I want it to look like after the "magic":

     |  Col0  |  Col1  |  Col3  |  Col6  |  Col43 |
---------------------------------------------------
RowA |   123  |    1   |   54   |    6   |    4   |

Is there a built-in function for such things in C#? And if not, how could I get started with this?

+1  A: 

You'll probably need to implement your IComparer<T>, as "natural" order would be: Col0, Col1, Col3, Col43 and Col6. ("4" comes before "6")

Rubens Farias
+1  A: 

You don't need to sort the columns in the DataTable object, just copy the column names to an array and sort the array. Then use the array to access the column values in the right order.

Sample:

class Program
    {
        static void Main(string[] args)
        {
            var dt = new DataTable { Columns = { "A3", "A2", "B1", "B3", "B2", "A1" } };
            dt.BeginLoadData();
            dt.Rows.Add("A3val", "A2val", "B1val", "B3val", "B2val", "A1val");
            dt.EndLoadData();

            string[] names=new string[dt.Columns.Count];
            for (int i = 0; i < dt.Columns.Count;i++ )
            {
                names[i] = dt.Columns[i].ColumnName;
            }
            Array.Sort(names);

            foreach (var name in names)
            {
                Console.Out.WriteLine("{0}={1}", name, dt.Rows[0][name]);
            }
            Console.ReadLine();
        }
Ariel Popovsky
Okay, I know what you mean, and it would work, quite well, but it's not quite... "genuine"... Still, thanks for the advice.
ApoY2k
If you want something more "genuine" perhaps you can work on the other end. Who is producing the datatable and why the columns are not ordered? You probably can't control that query or process but ir you can, then sort the columns there.
Ariel Popovsky
Problem is, that I can't control the order in which the user adds new columns to the data-table. So, e.g. if he adds a new column that should be in between two already existing ones, but is added as last column, I'd have to compare each one until it fits :-/
ApoY2k
Still, I like the idea. I'll add the sort-thing to the `get`-accessor of the string[], so it's done without much overhead. Nice idea!
ApoY2k
What about sorting when the user adds the column and setting the ordinal like JBrooks suggested. Suppose you have cols "A1", "A4" and user adds "A2", you go through the column names look for the place to insert the new column and set the ordinal manually. You should check what happens when you set the ordinal to an already existing value, maybe the are all renumbered automatically or maybe you have to increase the ordinal on the following columns manually.
Ariel Popovsky
+2  A: 

You can do the column sorting in the table itself:

dt.Columns["Col0"].SetOrdinal(0);
dt.Columns["Col1"].SetOrdinal(1);
dt.Columns["Col2"].SetOrdinal(2);
JBrooks