1 Previous Next 

DataTable: Change order of columns (Net 2.0)


This sample shows, beside the changes of the order of columns the use of the method: DataView.ToTable.

In  Net 2.0 came by this an often asked question from Net 1.x easy to do.

To test this sample you need a windowsform project with one DataGridView on the form


Public Class Form1
'Sample for VB 2005 and later
   Private Sub Form1_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
       Dim dt1 As New DataTable("myTable")
       dt1.Columns.Add("Count", GetType(System.Int32))
       dt1.Columns.Add("Date", GetType(System.DateTime))
       For i As Integer = 0 To 24
           dt1.LoadDataRow(New Object() _
                {i, New Date(2011, 1, 1).AddMonths(i)}, True)
       Next
       DataGridView1.DataSource = dt1
       'In the DataTable is a build in DataView with the property name
       'Defaultview
       Dim dt2 = dt1.DefaultView.ToTable(False, "Date", "Count")
       DataGridView2.DataSource = dt2
    End Sub
End Class




1 Previous Next