Solved

Whats the easiest and most effecient way to change the order of columns in a datatable

Posted on 2007-11-27
5
246 Views
Last Modified: 2013-11-26
Whats the easiest and most effecient way to change the order of columns in a datatable
0
Comment
Question by:paulCardiff
5 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 20360265
In a DataTable?  (Not an html table in a webpage?)  

I'm not an ASP.NET guy, but here's how you can do it with a DataTable:

'You can use the ToTable() method of the DataTable's DataView, and specifiy which columns you want:
        'Create a table for example:
        Dim table1 As New DataTable()
        table1.Columns.Add("Col1")
        table1.Columns.Add("Col2")
        table1.Columns.Add("Col3")

        'Move the contents to a new table, with the columns in the specified order:
        Dim table2 As DataTable
        table2 = table1.DefaultView.ToTable(False, New String() {"Col3", "Col2", "Col1"})

        'Loop through each column and see that it is in the specified order:
        For Each col As DataColumn In table2.Columns
            Debug.WriteLine(col.ColumnName)

        Next



        Stop
0
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 20360282
As an alternative, you can also use the SetOrdinal method to change the column index, without
copying data to another table as the example above demonstrates:

        Dim table1 As New DataTable()
        table1.Columns.Add("Col1")
        table1.Columns.Add("Col2")
        table1.Columns.Add("Col3")

        table1.Columns("Col1").SetOrdinal(2)
        table1.Columns("Col2").SetOrdinal(1)
        table1.Columns("Col3").SetOrdinal(0)

        For Each col As DataColumn In table1.Columns
            Debug.WriteLine(col.ColumnName)

        Next

0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20360321
+1 for VBRocks solution, but we might be able to do even better than that: if the datatable is created as the result of an SQL query or databinding to a table you should be able to specify a select in the order you want.
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20360572
good comment from jcoehoorn, but depends on the task he's doing! +2 for VBRocks

perhaps he should try to explain in more detail what he's trying to achieve


vbturbo
0
 
LVL 8

Expert Comment

by:Autoeforms
ID: 20360647
lets take another crack

my guess is your are doing a select * tablename and displaying it in the grid and don't like the column order

while the listed methods will work you can and should be more precise in your sql

for example: select col2, col3, col1, col5 from tablename

this will not only give you the columns in the order you want but will also execute faster.

cheers
greg
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question