Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
loadDatatable()
End Sub
Sub loadDatatable()
Dim dt As New DataTable("datatbl")
dt.Columns.Add("col1")
dt.Columns.Add("col2")
dt.Columns.Add("col3")
Dim i As Integer, a, b, c As String
For i = 1 To 5
a = "ia" & i.ToString
b = "ib" & i.ToString
c = "ic" & i.ToString
dt.Rows.Add(NewString() {a, b, c})
Next
DataGrid1.DataSource = dt
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
changeHeader()
End Sub
Sub changeHeader()
' Create new Table Style.
Dim ts As New DataGridTableStyle
' Allocate it a mapping name
ts.MappingName = "datatbl" ' Note this is the same name as the Table
' Get rid of the default TableStyle
DataGrid1.TableStyles.Clear()
' Add this Style to the TableStyles Collection
DataGrid1.TableStyles.Add(ts)
' Assign New Widths and Col Header Text to DataGrid columns.
With DataGrid1.TableStyles("datatbl")
.GridColumnStyles(0).Width = 100 ' First column, width
.GridColumnStyles(0).HeaderText() = "column A" ' Heading
.GridColumnStyles(1).Width = 100 ' First column, width
.GridColumnStyles(1).HeaderText() = "column B" ' Heading
.GridColumnStyles(2).Width = 100 ' First column, width
.GridColumnStyles(2).HeaderText() = "column C" ' Heading
End With
End Sub