Solved

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

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net mvc , views, hidden values ? 2 22
Visual Studio npm 1 29
Header Font Size in Grid View 6 22
CSS for Popup in ASP.NET 4 10
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

679 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