⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bindingdemoform11.vb

📁 是一个用SQL和VB.NET来做的源码。
💻 VB
字号:
' 导入命名空间
Imports System.Data.SqlClient
Public Class BindingDemoForm11
    Inherits System.Windows.Forms.Form

    ' 数据集对象的类层级建立
    Dim ds As DataSet = New DataSet()

    ' 关联性连接对象的类层级声明
    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 Label1 As System.Windows.Forms.Label
    Friend WithEvents ComboBoxCategory As System.Windows.Forms.ComboBox
    Friend WithEvents DataGridProduct As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.ComboBoxCategory = New System.Windows.Forms.ComboBox()
        Me.DataGridProduct = New System.Windows.Forms.DataGrid()
        CType(Me.DataGridProduct, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label1.ForeColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(0, Byte), CType(192, Byte))
        Me.Label1.Location = New System.Drawing.Point(93, 11)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(280, 24)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "请选择您所要查看的产品类别:"
        '
        'ComboBoxCategory
        '
        Me.ComboBoxCategory.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.ComboBoxCategory.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.ComboBoxCategory.ForeColor = System.Drawing.Color.Red
        Me.ComboBoxCategory.Location = New System.Drawing.Point(377, 9)
        Me.ComboBoxCategory.Name = "ComboBoxCategory"
        Me.ComboBoxCategory.Size = New System.Drawing.Size(176, 28)
        Me.ComboBoxCategory.TabIndex = 1
        '
        'DataGridProduct
        '
        Me.DataGridProduct.AlternatingBackColor = System.Drawing.Color.Lavender
        Me.DataGridProduct.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.DataGridProduct.BackColor = System.Drawing.Color.WhiteSmoke
        Me.DataGridProduct.BackgroundColor = System.Drawing.Color.LightGray
        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(18, 52)
        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(604, 315)
        Me.DataGridProduct.TabIndex = 2
        '
        'BindingDemoForm11
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 18)
        Me.ClientSize = New System.Drawing.Size(640, 374)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.ComboBoxCategory, Me.DataGridProduct})
        Me.Name = "BindingDemoForm11"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "复杂的数据绑定---DataGrid 控件"
        CType(Me.DataGridProduct, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub BindingDemoForm11_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 设定窗体的最小大小
        Me.MinimumSize = New Size(648, 414)

        ' 建立一个连接字符串
        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 myAD As SqlDataAdapter = New SqlDataAdapter(strSql, myConnection)


        ' 将数据填入数据集
        myAD.Fill(ds, "产品类别")

        ' 重新指定用来提取数据源的数据记录的 SELECT 语句
        myAD.SelectCommand.CommandText = "SELECT 产品编号, 产品, 供货商编号, 类别编号, 单位数量, 单价, 库存量, 已订购量, 安全存量, 不再销售 FROM 产品资料"

        ' 将数据填入数据集内名称为 产品资料 的数据表
        myAD.Fill(ds, "产品资料")

        ' 声明用来将数据集内的 产品类别 数据表连接至 产品资料 数据表的父字段与子字段
        Dim parentcol As DataColumn
        Dim childcol As DataColumn
        parentcol = ds.Tables("产品类别").Columns("类别编号")
        childcol = ds.Tables("产品资料").Columns("类别编号")

        ' 建立用来连接 产品类别 数据表与 产品资料 数据表的 DataRelation 对象,其名称为 每一种类的产品 。
        rel = New DataRelation _
            ("每一种类的产品", parentcol, childcol)
        ds.Relations.Add(rel)

        ' 设定 ComboBox 控件的数据源
        ComboBoxCategory.DataSource = ds
        ' 设定 ComboBox 控件的显示字段
        ComboBoxCategory.DisplayMember = "产品类别.类别名称"

        ' 将 DataGrid 控件绑定至 DataRelation 对象(亦即 每一种类的产品 )
        DataGridProduct.SetDataBinding(ds, "产品类别.每一种类的产品")

        myConnection.Close()

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -