Public Class Form1
Inherits System.Windows.Forms.Form
'[定义数据集]
'Dim dsCtf As DataSet
Dim dsCtf As New DataSet("dsCtf")
Dim tabShowDataGrid As New DataTable("showDataGrid") '用于显示datagrid的内存表
Private TablesAlreadyAdded As Boolean '是否加入了表头
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents btnCreateTable As System.Windows.Forms.Button
Friend WithEvents btnshowTable As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.btnCreateTable = New System.Windows.Forms.Button
Me.btnshowTable = New System.Windows.Forms.Button
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 16)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(448, 136)
Me.DataGrid1.TabIndex = 0
'
'btnCreateTable
'
Me.btnCreateTable.Location = New System.Drawing.Point(176, 168)
Me.btnCreateTable.Name = "btnCreateTable"
Me.btnCreateTable.Size = New System.Drawing.Size(112, 32)
Me.btnCreateTable.TabIndex = 1
Me.btnCreateTable.Text = "建立内存表"
'
'btnshowTable
'
Me.btnshowTable.Location = New System.Drawing.Point(176, 232)
Me.btnshowTable.Name = "btnshowTable"
Me.btnshowTable.Size = New System.Drawing.Size(112, 32)
Me.btnshowTable.TabIndex = 2
Me.btnshowTable.Text = "显示内存表"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(464, 285)
Me.Controls.Add(Me.btnshowTable)
Me.Controls.Add(Me.btnCreateTable)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnCreateTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTable.Click
'建表
createTab()
'填充数据
addInfo()
'提示,此提示应该由建表与填充数据执行成功后才能显示,本例中没有
MessageBox.Show("建立内存表成功,请点击显示进行查看")
End Sub
'[建立内存表]用于显示datagrid内容的表结构
Private Sub createTab()
If dsCtf.Tables.Contains("showDataGrid") = False Then
dsCtf.Tables.Add(tabShowDataGrid)
End If
'在内存表showDataGrid中建立5个列
Dim cCountID As New DataColumn("cCountID") 'add the countyID
Dim cCountName As New DataColumn("cCountName") 'add the countyName
Dim iLastStartNO As New DataColumn("iLastStartNo") 'add the lastNo
Dim iEndNo As New DataColumn("iEndNo") 'add the lastEndNo
Dim iNowNo As New DataColumn("iNowNo") 'add the inowno
'把这些列加入到表中
If tabShowDataGrid.Columns.Contains("cCountID") = False Then
tabShowDataGrid.Columns.Add(cCountID)
End If
If tabShowDataGrid.Columns.Contains("cCountName") = False Then
tabShowDataGrid.Columns.Add(cCountName)
End If
If tabShowDataGrid.Columns.Contains("iLastStartNO") = False Then
tabShowDataGrid.Columns.Add(iLastStartNO)
End If
If tabShowDataGrid.Columns.Contains("iEndNo") = False Then
tabShowDataGrid.Columns.Add(iEndNo)
End If
If tabShowDataGrid.Columns.Contains("iNowNo") = False Then
tabShowDataGrid.Columns.Add(iNowNo)
End If
End Sub
'给内存表加入数据库,并在datagrid中显示
Private Sub addInfo()
'清空表中的数据
tabShowDataGrid.Clear()
Dim RowData As Data.DataRow
Dim iRow As Int16 '行数
'加入数据
For iRow = 0 To 3
RowData = tabShowDataGrid.NewRow
RowData("cCountID") = iRow
RowData("cCountName") = CStr(iRow) & " " & "cCountName"
RowData("iLastStartNO") = iRow
RowData("iEndNo") = iRow + 1
RowData("iNowNo") = iRow + 2
tabShowDataGrid.Rows.Add(RowData)
Next
'加入表头
If TablesAlreadyAdded = True Then Exit Sub
AddCustomDataTableStyle()
End Sub
' 加入表头,加入datagrid显示内容的表头
Private Sub AddCustomDataTableStyle()
'可以通过 DataGridTableStyle 控制每个 DataTable 的网格的外观。
'若要指定在显示来自特定 DataTable 的数据时所使用的 DataGridTableStyle,
'请将 MappingName 设置为某 DataTable 的 TableName
Dim ts1 As New DataGridTableStyle
ts1.MappingName = "showDataGrid"
'获取或设置网格中奇数行的背景色
ts1.AlternatingBackColor = Color.LightGray
'参看DataGridTextBoxColumn 类
Dim TextCol As New DataGridTextBoxColumn
'DataGridColumnStyle.MappingName 属性:获取或设置用于将列样式映射到数据成员的名称。
TextCol.MappingName = "cCountID"
TextCol.HeaderText = "编号"
TextCol.Width = Len(TextCol.HeaderText) * 20
ts1.GridColumnStyles.Add(TextCol)
Dim TextCol1 As New DataGridTextBoxColumn
TextCol1.MappingName = "cCountName"
TextCol1.HeaderText = "名称"
TextCol1.Width = Len(TextCol1.HeaderText) * 50
ts1.GridColumnStyles.Add(TextCol1)
Dim TextCol2 As New DataGridTextBoxColumn
TextCol2.MappingName = "iLastStartNO"
TextCol2.HeaderText = "上次起始号码"
TextCol2.Width = Len(TextCol2.HeaderText) * 14
ts1.GridColumnStyles.Add(TextCol2)
Dim TextCol3 As New DataGridTextBoxColumn
TextCol3.MappingName = "iEndNO"
TextCol3.HeaderText = "上次结束号码"
TextCol3.Width = Len(TextCol3.HeaderText) * 14
ts1.GridColumnStyles.Add(TextCol3)
Dim TextCol4 As New DataGridTextBoxColumn
TextCol4.MappingName = "iNowNO"
TextCol4.HeaderText = "本次起始号码"
TextCol4.Width = Len(TextCol4.HeaderText) * 14
ts1.GridColumnStyles.Add(TextCol4)
DataGrid1.TableStyles.Add(ts1)
TablesAlreadyAdded = True
End Sub
Private Sub btnshowTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnshowTable.Click
'DataGrid.SetDataBinding 方法:在运行时设置 DataSource 和 DataMember 属性。
DataGrid1.SetDataBinding(dsCtf, "showDataGrid")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class