form1.vb
来自「vb 应用实例 简单应用 dddddddddddddddddddddddddd」· VB 代码 · 共 266 行
VB
266 行
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form 设计工具产生的程序代码 "
Public Sub New()
MyBase.New()
'此呼叫为 Windows Form 设计工具的必要项。
InitializeComponent()
'在 InitializeComponent() 呼叫之后加入所有的初始设定
End Sub
'Form 覆写 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 Form 设计工具的必要项
Private components As System.ComponentModel.IContainer
'注意: 以下为 Windows Form 设计工具所需的程序
'您可以使用 Windows Form 设计工具进行修改。
'请勿使用程序代码编辑器来修改这些程序。
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents Button5 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.Button4 = New System.Windows.Forms.Button()
Me.Button5 = 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(195, 8)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(399, 256)
Me.DataGrid1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(10, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(174, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "预设"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(10, 40)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(174, 24)
Me.Button2.TabIndex = 2
Me.Button2.Text = "DataGrid properties"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(10, 104)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(174, 24)
Me.Button4.TabIndex = 4
Me.Button4.Text = "DataGridTextBoxColumn"
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(10, 72)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(174, 24)
Me.Button5.TabIndex = 5
Me.Button5.Text = "DataGridTableStyle"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(614, 278)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button5, Me.Button4, Me.Button2, Me.Button1, Me.DataGrid1})
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "DataGrid Format"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ResetDataGrid()
BindDataGrid()
End Sub
Private Sub BindDataGrid()
'SQL Server
'Dim conn As New SqlConnection("Server=localhost;DataBase=northwind;Integrated Security=SSPI")
'MSDE
Dim conn As New SqlConnection("Server=(local);DataBase=northwind;Integrated Security=SSPI")
Dim ds As DataSet
Dim da As SqlDataAdapter
Try
'da = New SqlDataAdapter("SELECT ProductID, ProductName, UnitPrice, UnitsInStock FROM Products", conn)
Dim cmd1 As New SqlCommand("SELECT ProductID, ProductName, UnitPrice, UnitsInStock FROM Products", conn)
da = New SqlDataAdapter(cmd1)
ds = New DataSet()
da.Fill(ds, "Products")
DataGrid1.DataSource = ds.Tables(0)
Catch e1 As Exception
MsgBox("无法读取. " & e1.ToString)
Finally
If (conn.State = ConnectionState.Open) Then
conn.Close()
End If
End Try
End Sub
Private Sub ResetDataGrid()
With DataGrid1
.BackgroundColor = SystemColors.InactiveCaptionText
.CaptionText = ""
.CaptionBackColor = SystemColors.ActiveCaption
.TableStyles.Clear()
.ResetAlternatingBackColor()
.ResetBackColor()
.ResetForeColor()
.ResetGridLineColor()
.ResetHeaderBackColor()
.ResetHeaderFont()
.ResetHeaderForeColor()
.ResetSelectionBackColor()
.ResetSelectionForeColor()
.ResetText()
End With
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ResetDataGrid()
BindDataGrid()
'直接由 DataGrid properties 设定 DataGrid format
With DataGrid1
.BackColor = Color.GhostWhite
.BackgroundColor = Color.Lavender
.BorderStyle = BorderStyle.None
.CaptionBackColor = Color.RoyalBlue
.CaptionFont = New Font("Tahoma", 10.0!, FontStyle.Bold)
.CaptionForeColor = Color.Bisque
.CaptionText = "Northwind Products"
.Font = New Font("Tahoma", 8.0!)
.ParentRowsBackColor = Color.Lavender
.ParentRowsForeColor = Color.MidnightBlue
End With
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
ResetDataGrid()
BindDataGrid()
'由 DataGridTableStyle 设定 DataGrid format
Dim dgts1 As New DataGridTableStyle()
With dgts1
.AlternatingBackColor = Color.GhostWhite
.BackColor = Color.GhostWhite
.ForeColor = Color.MidnightBlue
.GridLineColor = Color.RoyalBlue
.HeaderBackColor = Color.MidnightBlue
.HeaderFont = New Font("Tahoma", 8.0!, FontStyle.Bold)
.HeaderForeColor = Color.Lavender
.SelectionBackColor = Color.Teal
.SelectionForeColor = Color.PaleGreen
'需设定 MappingName property, DataGridColumnStyle 才有效
.MappingName = "Products"
.PreferredColumnWidth = 125
.PreferredRowHeight = 15
End With
DataGrid1.TableStyles.Add(dgts1)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ResetDataGrid()
BindDataGrid()
'使用 DataGridTextBoxColumn class 格式化每一个字段
Dim dgts2 As New DataGridTableStyle()
With dgts2
.AlternatingBackColor = Color.GhostWhite
.BackColor = Color.GhostWhite
.ForeColor = Color.MidnightBlue
.GridLineColor = Color.RoyalBlue
.HeaderBackColor = Color.MidnightBlue
.HeaderFont = New Font("Tahoma", 8.0!, FontStyle.Bold)
.HeaderForeColor = Color.Lavender
.SelectionBackColor = Color.Teal
.SelectionForeColor = Color.PaleGreen
'需设定 MappingName property, DataGridColumnStyle 才有效
.MappingName = "Products"
.PreferredColumnWidth = 125
.PreferredRowHeight = 15
End With
'使用 DataGridTextBoxColumn class 格式化每一个字段
Dim grdColStyle1 As New DataGridTextBoxColumn()
With grdColStyle1
.HeaderText = "产品代号"
.MappingName = "ProductID"
.Width = 50
End With
Dim grdColStyle2 As New DataGridTextBoxColumn()
With grdColStyle2
.HeaderText = "产品名称"
.MappingName = "ProductName"
End With
Dim grdColStyle3 As New DataGridTextBoxColumn()
With grdColStyle3
.HeaderText = "单价"
.MappingName = "UnitPrice"
.Width = 75
.ReadOnly = True
End With
Dim grdColStyle4 As New DataGridTextBoxColumn()
With grdColStyle4
.HeaderText = "库存量"
.MappingName = "UnitsInStock"
.Width = 75
.Alignment = HorizontalAlignment.Center
End With
dgts2.GridColumnStyles.AddRange(New DataGridColumnStyle() {grdColStyle1, grdColStyle2, grdColStyle3, grdColStyle4})
DataGrid1.TableStyles.Add(dgts2)
End Sub
End Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?