Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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