moveing columns on a datatable

  • Thread starter Thread starter Dibs
  • Start date Start date
D

Dibs

Hi all,

short question.
is there anyway of changing the order of a datatable's columns?

cheers,
Dibs
 
Dibs,

I was curious if I could do it.

It does, however don't ask what I made from it, maybe somebody has a much
more simple method.

\\\\Needs a datagrid on a form to test.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As DataTable = CreateTables()
Dim dtAr(dt.Columns.Count - 1) As DataColumn
Dim dtFake As DataTable = dt.Clone
For i As Integer = dtFake.Columns.Count - 1 To 0 Step -1
dtAr(i) = dtFake.Columns(i)
dtFake.Columns.RemoveAt(i)
Next
Dim dtNew As New DataTable
dtNew.Columns.Add(dtAr(1))
dtNew.Columns.Add(dtAr(0))
For i As Integer = 0 To dt.Rows.Count - 1
dtNew.Rows.Add(dtNew.NewRow)
For y As Integer = 0 To dtNew.Columns.Count - 1
dtNew.Rows(i)(dtNew.Columns(y)) = _
dt.Rows(i)(dtNew.Columns(y).ColumnName)
Next
Next
DataGrid1.DataSource = dtNew.DefaultView
End Sub
'Sample datatable
Private Function CreateTables() As DataTable
Dim dt As New DataTable("Persons")
dt.Columns.Add("Name")
dt.Columns.Add("USA", GetType(System.Boolean))
dt.LoadDataRow(New Object() {"Ken Tucker", True}, True)
dt.LoadDataRow(New Object() {"Cor Ligthert", False}, True)
Return dt
End Function
///

I hope this helps,

Cor
 
cheers Cor

Well that seemed to do the job, it's an impressively complicated way of
doing something that you'd have thought might be relatively easy.

Dibs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top