在内存中建表在DagaGrid中显示

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

  1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
  2Me.DataGrid1 = New System.Windows.Forms.DataGrid   
  3Me.btnCreateTable = New System.Windows.Forms.Button   
  4Me.btnshowTable = New System.Windows.Forms.Button   
  5CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()   
  6Me.SuspendLayout()   
  7'   
  8'DataGrid1   
  9'   
 10Me.DataGrid1.DataMember = ""   
 11Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText   
 12Me.DataGrid1.Location = New System.Drawing.Point(8, 16)   
 13Me.DataGrid1.Name = "DataGrid1"   
 14Me.DataGrid1.Size = New System.Drawing.Size(448, 136)   
 15Me.DataGrid1.TabIndex = 0   
 16'   
 17'btnCreateTable   
 18'   
 19Me.btnCreateTable.Location = New System.Drawing.Point(176, 168)   
 20Me.btnCreateTable.Name = "btnCreateTable"   
 21Me.btnCreateTable.Size = New System.Drawing.Size(112, 32)   
 22Me.btnCreateTable.TabIndex = 1   
 23Me.btnCreateTable.Text = "建立内存表"   
 24'   
 25'btnshowTable   
 26'   
 27Me.btnshowTable.Location = New System.Drawing.Point(176, 232)   
 28Me.btnshowTable.Name = "btnshowTable"   
 29Me.btnshowTable.Size = New System.Drawing.Size(112, 32)   
 30Me.btnshowTable.TabIndex = 2   
 31Me.btnshowTable.Text = "显示内存表"   
 32'   
 33'Form1   
 34'   
 35Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)   
 36Me.ClientSize = New System.Drawing.Size(464, 285)   
 37Me.Controls.Add(Me.btnshowTable)   
 38Me.Controls.Add(Me.btnCreateTable)   
 39Me.Controls.Add(Me.DataGrid1)   
 40Me.Name = "Form1"   
 41Me.Text = "Form1"   
 42CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()   
 43Me.ResumeLayout(False) 
 44
 45End Sub 
 46
 47#End Region 
 48
 49Private Sub btnCreateTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTable.Click 
 50
 51'建表   
 52createTab() 
 53
 54'填充数据   
 55addInfo() 
 56
 57'提示,此提示应该由建表与填充数据执行成功后才能显示,本例中没有   
 58MessageBox.Show("建立内存表成功,请点击显示进行查看")   
 59End Sub 
 60
 61'[建立内存表]用于显示datagrid内容的表结构   
 62Private Sub createTab() 
 63
 64If dsCtf.Tables.Contains("showDataGrid") = False Then   
 65dsCtf.Tables.Add(tabShowDataGrid)   
 66End If 
 67
 68'在内存表showDataGrid中建立5个列   
 69Dim cCountID As New DataColumn("cCountID") 'add the countyID   
 70Dim cCountName As New DataColumn("cCountName") 'add the countyName   
 71Dim iLastStartNO As New DataColumn("iLastStartNo") 'add the lastNo   
 72Dim iEndNo As New DataColumn("iEndNo") 'add the lastEndNo   
 73Dim iNowNo As New DataColumn("iNowNo") 'add the inowno 
 74
 75'把这些列加入到表中   
 76If tabShowDataGrid.Columns.Contains("cCountID") = False Then   
 77tabShowDataGrid.Columns.Add(cCountID)   
 78End If   
 79If tabShowDataGrid.Columns.Contains("cCountName") = False Then   
 80tabShowDataGrid.Columns.Add(cCountName)   
 81End If   
 82If tabShowDataGrid.Columns.Contains("iLastStartNO") = False Then   
 83tabShowDataGrid.Columns.Add(iLastStartNO)   
 84End If   
 85If tabShowDataGrid.Columns.Contains("iEndNo") = False Then   
 86tabShowDataGrid.Columns.Add(iEndNo)   
 87End If   
 88If tabShowDataGrid.Columns.Contains("iNowNo") = False Then   
 89tabShowDataGrid.Columns.Add(iNowNo)   
 90End If 
 91
 92End Sub 
 93
 94'给内存表加入数据库,并在datagrid中显示   
 95Private Sub addInfo() 
 96
 97'清空表中的数据   
 98tabShowDataGrid.Clear() 
 99
100Dim RowData As Data.DataRow   
101Dim iRow As Int16 '行数 
102
103'加入数据   
104For iRow = 0 To 3 
105
106RowData = tabShowDataGrid.NewRow 
107
108RowData("cCountID") = iRow   
109RowData("cCountName") = CStr(iRow) &amp; " " &amp; "cCountName"   
110RowData("iLastStartNO") = iRow   
111RowData("iEndNo") = iRow + 1   
112RowData("iNowNo") = iRow + 2 
113
114tabShowDataGrid.Rows.Add(RowData)   
115Next 
116
117'加入表头   
118If TablesAlreadyAdded = True Then Exit Sub   
119AddCustomDataTableStyle()   
120End Sub 
121
122' 加入表头,加入datagrid显示内容的表头   
123Private Sub AddCustomDataTableStyle() 
124
125'可以通过 DataGridTableStyle 控制每个 DataTable 的网格的外观。   
126'若要指定在显示来自特定 DataTable 的数据时所使用的 DataGridTableStyle,   
127'请将 MappingName 设置为某 DataTable 的 TableName   
128Dim ts1 As New DataGridTableStyle   
129ts1.MappingName = "showDataGrid" 
130
131'获取或设置网格中奇数行的背景色   
132ts1.AlternatingBackColor = Color.LightGray 
133
134'参看DataGridTextBoxColumn 类   
135Dim TextCol As New DataGridTextBoxColumn   
136'DataGridColumnStyle.MappingName 属性:获取或设置用于将列样式映射到数据成员的名称。   
137TextCol.MappingName = "cCountID"   
138TextCol.HeaderText = "编号"   
139TextCol.Width = Len(TextCol.HeaderText) * 20   
140ts1.GridColumnStyles.Add(TextCol) 
141
142Dim TextCol1 As New DataGridTextBoxColumn   
143TextCol1.MappingName = "cCountName"   
144TextCol1.HeaderText = "名称"   
145TextCol1.Width = Len(TextCol1.HeaderText) * 50   
146ts1.GridColumnStyles.Add(TextCol1) 
147
148Dim TextCol2 As New DataGridTextBoxColumn   
149TextCol2.MappingName = "iLastStartNO"   
150TextCol2.HeaderText = "上次起始号码"   
151TextCol2.Width = Len(TextCol2.HeaderText) * 14   
152ts1.GridColumnStyles.Add(TextCol2) 
153
154Dim TextCol3 As New DataGridTextBoxColumn   
155TextCol3.MappingName = "iEndNO"   
156TextCol3.HeaderText = "上次结束号码"   
157TextCol3.Width = Len(TextCol3.HeaderText) * 14   
158ts1.GridColumnStyles.Add(TextCol3) 
159
160Dim TextCol4 As New DataGridTextBoxColumn   
161TextCol4.MappingName = "iNowNO"   
162TextCol4.HeaderText = "本次起始号码"   
163TextCol4.Width = Len(TextCol4.HeaderText) * 14   
164ts1.GridColumnStyles.Add(TextCol4) 
165
166DataGrid1.TableStyles.Add(ts1) 
167
168TablesAlreadyAdded = True   
169End Sub 
170
171Private Sub btnshowTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnshowTable.Click 
172
173'DataGrid.SetDataBinding 方法:在运行时设置 DataSource 和 DataMember 属性。   
174DataGrid1.SetDataBinding(dsCtf, "showDataGrid")   
175End Sub 
176
177Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
178
179End Sub   
180End Class</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus