📄 demoform4.vb
字号:
Imports System.Data.SqlClient
Public Class DemoForm4
Inherits System.Windows.Forms.Form
' 数据集对象的模块层级声明
Dim ds As DataSet = New DataSet()
' 查看表对象的模块层级声明
Dim dv As DataView
' 关联性连接对象的模块层级声明
Dim rel As DataRelation
#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 ComboBoxCategory As System.Windows.Forms.ComboBox
Friend WithEvents DataGridProduct As System.Windows.Forms.DataGrid
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ComboBoxCategory = New System.Windows.Forms.ComboBox()
Me.DataGridProduct = New System.Windows.Forms.DataGrid()
Me.Label1 = New System.Windows.Forms.Label()
CType(Me.DataGridProduct, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'ComboBoxCategory
'
Me.ComboBoxCategory.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBoxCategory.Location = New System.Drawing.Point(261, 10)
Me.ComboBoxCategory.Name = "ComboBoxCategory"
Me.ComboBoxCategory.Size = New System.Drawing.Size(121, 23)
Me.ComboBoxCategory.TabIndex = 47
'
'DataGridProduct
'
Me.DataGridProduct.AlternatingBackColor = System.Drawing.Color.Lavender
Me.DataGridProduct.BackColor = System.Drawing.Color.WhiteSmoke
Me.DataGridProduct.BackgroundColor = System.Drawing.Color.LightGray
Me.DataGridProduct.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.DataGridProduct.CaptionBackColor = System.Drawing.Color.LightSteelBlue
Me.DataGridProduct.CaptionForeColor = System.Drawing.Color.MidnightBlue
Me.DataGridProduct.CaptionText = "产品基本数据"
Me.DataGridProduct.DataMember = ""
Me.DataGridProduct.FlatMode = True
Me.DataGridProduct.Font = New System.Drawing.Font("Tahoma", 8.0!)
Me.DataGridProduct.ForeColor = System.Drawing.Color.MidnightBlue
Me.DataGridProduct.GridLineColor = System.Drawing.Color.Gainsboro
Me.DataGridProduct.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None
Me.DataGridProduct.HeaderBackColor = System.Drawing.Color.MidnightBlue
Me.DataGridProduct.HeaderFont = New System.Drawing.Font("Tahoma", 8.0!, System.Drawing.FontStyle.Bold)
Me.DataGridProduct.HeaderForeColor = System.Drawing.Color.WhiteSmoke
Me.DataGridProduct.LinkColor = System.Drawing.Color.Teal
Me.DataGridProduct.Location = New System.Drawing.Point(8, 44)
Me.DataGridProduct.Name = "DataGridProduct"
Me.DataGridProduct.ParentRowsBackColor = System.Drawing.Color.Gainsboro
Me.DataGridProduct.ParentRowsForeColor = System.Drawing.Color.MidnightBlue
Me.DataGridProduct.SelectionBackColor = System.Drawing.Color.CadetBlue
Me.DataGridProduct.SelectionForeColor = System.Drawing.Color.WhiteSmoke
Me.DataGridProduct.Size = New System.Drawing.Size(560, 359)
Me.DataGridProduct.TabIndex = 48
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(52, 12)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(210, 18)
Me.Label1.TabIndex = 49
Me.Label1.Text = "请选择您所要查看的产品类别:"
'
'DemoForm4
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 18)
Me.ClientSize = New System.Drawing.Size(579, 410)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.DataGridProduct, Me.ComboBoxCategory})
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "DemoForm4"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "DemoForm4"
CType(Me.DataGridProduct, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub DemoForm4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' 建立一个连接字符串
Dim strConnection As String = "Server=(local)\NetSDK;Database=北风贸易;Integrated Security=SSPI"
' 建立一个查询命令字符串
Dim strSql As String = "SELECT 类别编号, 类别名称 FROM 产品类别"
' 建立一个数据连接
Dim myConnection As SqlConnection = _
New SqlConnection(strConnection)
' 建立一个数据适配器以便针对数据源执行 SELECT 语句来提取出要填入数据集的数据记录
Dim myCommand As SqlDataAdapter = New SqlDataAdapter(strSql, myConnection)
Try
' 将数据填入数据集
myCommand.Fill(ds, "产品类别")
'Re-specify the SQL string for the command and the data
'adapter to extract columns from the Products table
myCommand.SelectCommand.CommandText = "SELECT 产品编号, 产品, 供货商编号, 类别编号, 单位数量, 单价, 库存量, 已订购量, 安全存量, 不再销售 FROM 产品资料"
'dap1.SelectCommand = cmd1
'Create a datatable named Products in the das1 dataset
'based on an extract from the Products table in the
'northwind database
myCommand.Fill(ds, "产品资料")
'Declare and assign parent and child columns for
'relating the Products datatable to the Categories
'datatable in the das1 dataset
Dim parentcol As DataColumn
Dim childcol As DataColumn
parentcol = ds.Tables("产品类别").Columns("类别编号")
childcol = ds.Tables("产品资料").Columns("类别编号")
'Instantiate a datarelation between the
'Products and Categories datatables
rel = New DataRelation _
("CategoriesProducts", parentcol, childcol)
ds.Relations.Add(rel)
ComboBoxCategory.DataSource = ds.Tables("产品类别")
ComboBoxCategory.DisplayMember = "类别名称"
Me.BindingContext(ds, "产品类别").Position = 0
'Instantiate a dataview based on the Products datatable
'in the das1 dataset and filter the view on the basis of
'selectedindex value for a combobox.
dv = New DataView(ds.Tables("产品资料"))
Dim strFilter = "类别编号 = " & _
(ComboBoxCategory.SelectedIndex + 1).ToString
dv.RowFilter = strFilter
'Assign the view as the data source for a datagrid control.
DataGridProduct.DataSource = dv
' 设定窗体的最小大小
Me.MinimumSize = New Size(587, 450)
Catch ex As Exception
Throw ex
Finally
myConnection.Close()
End Try
End Sub
Private Sub ComboBoxCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxCategory.SelectedIndexChanged
'Assign filter based on selected item, and
'apply the filter to the dataview for the datagrid control.
Dim strFilter = "类别编号 = " & _
(ComboBoxCategory.SelectedIndex + 1).ToString
dv.RowFilter = strFilter
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -