Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
table = Sample.CreateTable
Me.DataGridView1.DataSource = table
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
With table.Columns.Add("序号", GetType(Integer))
.AutoIncrement = True
End With
For i As Integer = 0 To table.Rows.Count - 1
table.Rows(i).Item("序号") = i
Next
table.Columns("序号").ReadOnly = True
End Sub
End Class
Public Class Sample
Public Shared Function CreateTable() As DataTable
Dim table As New DataTable
With table
With .Columns
.Add("姓名")
.Add("年龄", GetType(Integer))
End With
.LoadDataRow(New Object() {"张三", 20}, True)
.LoadDataRow(New Object() {"李四", 21}, True)
.LoadDataRow(New Object() {"王五", 20}, True)
End With