Solved

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

Posted on 2007-11-27
5
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

630 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