Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' DataTable里只有Name列
Dim data As DataTable = New DataTable
data.Columns.Add("Name")
data.Rows.Add(New Object() {"zhangsan"})
data.Rows.Add(New Object() {"lisi"})
data.Rows.Add(New Object() {"wangwu"})
Me.DataGridView1.DataSource = data
End Sub
' 直接赋值。已经Design了一列——ID列。
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If DataGridView1.Columns(e.ColumnIndex).HeaderText = "ID" Then
e.Value = "001"
End If
End Sub
End Class