1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
Public Class Form1
Dim originalDataTable As DataTable
Dim resDT As DataTable
Private Sub btnTestDataTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestDataTable.Click
btnCloneDataTable.Enabled = False
'Cree DataTable test
originalDataTable = New DataTable
'Cree 5 colonnes
Dim col As DataColumn
For j As Integer = 0 To 4
col = New DataColumn
col.Caption = "ItemCol" & (j).ToString
col.ColumnName = "ItemCol" & (j).ToString
col.DataType = GetType(String)
originalDataTable.Columns.Add(col)
Next
'Fill avec 7 rows
Dim dr As DataRow
Dim k As Integer = 1
For i As Integer = 0 To 6
dr = originalDataTable.NewRow
For j As Integer = 0 To dr.ItemArray.Count - 1
dr.Item(j) = "Value" & k.ToString
k = k + 1
Next
originalDataTable.Rows.Add(dr)
Next
Me.DataGridView1.DataSource = originalDataTable
btnCloneDataTable.Enabled = True
End Sub
Private Sub btnCloneDataTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloneDataTable.Click
'cree un clone de myDatable pour eviter
'la double appartenance
'ce clone copie seulement les colonnes(sans donnees)
resDT = originalDataTable.Clone()
'copie les colonnes de resDT dans arrCol
Dim arrCol(resDT.Columns.Count) As DataColumn
resDT.Columns.CopyTo(arrCol, 0)
'un array vide pour les colonnes apres permutation
Dim arrPermut(resDT.Columns.Count) As DataColumn
'"permutator" on fait ce qu'on veut
For i As Integer = 0 To arrCol.Length - 1
arrPermut(i) = arrCol(i)
If i = 3 Then
'effectue permutation 0 & 3
arrPermut(0) = arrCol(i)
arrPermut(i) = arrCol(0)
ElseIf i = 4 Then
'effectue permutation 2 & 4
arrPermut(2) = arrCol(i)
arrPermut(i) = arrCol(2)
End If
Next
'Efface colonnes intiales
resDT.Columns.Clear()
'Ajoute à resDT colonnes permutees stores dans arrPermut
resDT.Columns.AddRange(arrPermut)
'mantenant on rapatrie les "data" de myDataTable(3 lignes de code)
For Each dr As DataRow In originalDataTable.Rows
resDT.ImportRow(dr)
Next
Me.DataGridView2.DataSource = resDT
Me.TextBox1.Clear()
For Each dc1 As DataColumn In resDT.Columns
Me.TextBox1.Text = Me.TextBox1.Text & dc1.ColumnName & " - " & dc1.Ordinal.ToString & vbCrLf
Next
End Sub
End Class |
Partager