Solved

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

Posted on 2007-11-27
5
243 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now