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

📄 bindingdemoform12.vb

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

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

    ' 关联性连接对象的类层级声明
    Dim rel As DataRelation

    ' CurrencyManager 对象的类层级声明
    Dim bmCategory As BindingManagerBase

#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 lblSupplierID As System.Windows.Forms.Label
    Friend WithEvents lblProduct As System.Windows.Forms.Label
    Friend WithEvents lblProductID As System.Windows.Forms.Label
    Friend WithEvents TextBoxCategoryID As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxDescription As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxCategoryName As System.Windows.Forms.TextBox
    Friend WithEvents btnEnd As System.Windows.Forms.Button
    Friend WithEvents btnNext As System.Windows.Forms.Button
    Friend WithEvents btnBack As System.Windows.Forms.Button
    Friend WithEvents btnFirst As System.Windows.Forms.Button
    Friend WithEvents TextBoxPosition As System.Windows.Forms.TextBox
    Friend WithEvents DataGridProduct As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(BindingDemoForm12))
        Me.lblSupplierID = New System.Windows.Forms.Label()
        Me.lblProduct = New System.Windows.Forms.Label()
        Me.lblProductID = New System.Windows.Forms.Label()
        Me.TextBoxCategoryID = New System.Windows.Forms.TextBox()
        Me.TextBoxDescription = New System.Windows.Forms.TextBox()
        Me.TextBoxCategoryName = New System.Windows.Forms.TextBox()
        Me.btnEnd = New System.Windows.Forms.Button()
        Me.btnNext = New System.Windows.Forms.Button()
        Me.btnBack = New System.Windows.Forms.Button()
        Me.btnFirst = New System.Windows.Forms.Button()
        Me.TextBoxPosition = New System.Windows.Forms.TextBox()
        Me.DataGridProduct = New System.Windows.Forms.DataGrid()
        CType(Me.DataGridProduct, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'lblSupplierID
        '
        Me.lblSupplierID.AutoSize = True
        Me.lblSupplierID.Location = New System.Drawing.Point(130, 46)
        Me.lblSupplierID.Name = "lblSupplierID"
        Me.lblSupplierID.Size = New System.Drawing.Size(40, 18)
        Me.lblSupplierID.TabIndex = 90
        Me.lblSupplierID.Text = "说明:"
        '
        'lblProduct
        '
        Me.lblProduct.AutoSize = True
        Me.lblProduct.Location = New System.Drawing.Point(295, 15)
        Me.lblProduct.Name = "lblProduct"
        Me.lblProduct.Size = New System.Drawing.Size(71, 18)
        Me.lblProduct.TabIndex = 88
        Me.lblProduct.Text = "类别名称:"
        '
        'lblProductID
        '
        Me.lblProductID.AutoSize = True
        Me.lblProductID.Location = New System.Drawing.Point(99, 15)
        Me.lblProductID.Name = "lblProductID"
        Me.lblProductID.Size = New System.Drawing.Size(71, 18)
        Me.lblProductID.TabIndex = 86
        Me.lblProductID.Text = "类别编号:"
        '
        'TextBoxCategoryID
        '
        Me.TextBoxCategoryID.ForeColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
        Me.TextBoxCategoryID.Location = New System.Drawing.Point(179, 12)
        Me.TextBoxCategoryID.Name = "TextBoxCategoryID"
        Me.TextBoxCategoryID.TabIndex = 87
        Me.TextBoxCategoryID.Text = ""
        '
        'TextBoxDescription
        '
        Me.TextBoxDescription.ForeColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
        Me.TextBoxDescription.Location = New System.Drawing.Point(179, 43)
        Me.TextBoxDescription.Name = "TextBoxDescription"
        Me.TextBoxDescription.Size = New System.Drawing.Size(374, 25)
        Me.TextBoxDescription.TabIndex = 91
        Me.TextBoxDescription.Text = ""
        '
        'TextBoxCategoryName
        '
        Me.TextBoxCategoryName.ForeColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
        Me.TextBoxCategoryName.Location = New System.Drawing.Point(375, 12)
        Me.TextBoxCategoryName.Name = "TextBoxCategoryName"
        Me.TextBoxCategoryName.Size = New System.Drawing.Size(177, 25)
        Me.TextBoxCategoryName.TabIndex = 89
        Me.TextBoxCategoryName.Text = ""
        '
        'btnEnd
        '
        Me.btnEnd.BackColor = System.Drawing.Color.White
        Me.btnEnd.Image = CType(resources.GetObject("btnEnd.Image"), System.Drawing.Bitmap)
        Me.btnEnd.Location = New System.Drawing.Point(427, 130)
        Me.btnEnd.Name = "btnEnd"
        Me.btnEnd.Size = New System.Drawing.Size(58, 27)
        Me.btnEnd.TabIndex = 85
        '
        'btnNext
        '
        Me.btnNext.BackColor = System.Drawing.Color.White
        Me.btnNext.Image = CType(resources.GetObject("btnNext.Image"), System.Drawing.Bitmap)
        Me.btnNext.Location = New System.Drawing.Point(347, 130)
        Me.btnNext.Name = "btnNext"
        Me.btnNext.Size = New System.Drawing.Size(58, 27)
        Me.btnNext.TabIndex = 84
        '
        'btnBack
        '
        Me.btnBack.BackColor = System.Drawing.Color.White
        Me.btnBack.Image = CType(resources.GetObject("btnBack.Image"), System.Drawing.Bitmap)
        Me.btnBack.Location = New System.Drawing.Point(267, 130)
        Me.btnBack.Name = "btnBack"
        Me.btnBack.Size = New System.Drawing.Size(58, 27)
        Me.btnBack.TabIndex = 83
        '
        'btnFirst
        '
        Me.btnFirst.BackColor = System.Drawing.Color.White
        Me.btnFirst.Image = CType(resources.GetObject("btnFirst.Image"), System.Drawing.Bitmap)
        Me.btnFirst.Location = New System.Drawing.Point(187, 130)
        Me.btnFirst.Name = "btnFirst"
        Me.btnFirst.Size = New System.Drawing.Size(58, 27)
        Me.btnFirst.TabIndex = 82
        '
        'TextBoxPosition
        '
        Me.TextBoxPosition.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.TextBoxPosition.ForeColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(0, Byte), CType(192, Byte))
        Me.TextBoxPosition.Location = New System.Drawing.Point(190, 86)
        Me.TextBoxPosition.Name = "TextBoxPosition"
        Me.TextBoxPosition.Size = New System.Drawing.Size(290, 25)
        Me.TextBoxPosition.TabIndex = 81
        Me.TextBoxPosition.Text = ""
        Me.TextBoxPosition.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        '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(7, 169)
        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(654, 397)
        Me.DataGridProduct.TabIndex = 80
        '
        'BindingDemoForm12
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 18)
        Me.ClientSize = New System.Drawing.Size(672, 576)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblSupplierID, Me.lblProduct, Me.lblProductID, Me.TextBoxCategoryID, Me.TextBoxDescription, Me.TextBoxCategoryName, Me.btnEnd, Me.btnNext, Me.btnBack, Me.btnFirst, Me.TextBoxPosition, Me.DataGridProduct})
        Me.Name = "BindingDemoForm12"
        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 BindingDemoForm12_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 设定窗体的最小大小
        Me.MinimumSize = New Size(680, 616)

        ' 建立一个连接字符串
        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)

        TextBoxCategoryID.DataBindings.Add("Text", ds, "产品类别.类别编号")
        TextBoxCategoryName.DataBindings.Add("Text", ds, "产品类别.类别名称")
        TextBoxDescription.DataBindings.Add("Text", ds, "产品类别.说明")

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

        ' 取得代表 产品类别 数据表的 CurrencyManager 对象
        bmCategory = Me.BindingContext(ds, "产品类别")

        ' 设定当引发 PositionChanged 事件时便执行事件处理例程 产品类别_PositionChanged
        AddHandler bmCategory.PositionChanged, AddressOf 产品类别_PositionChanged

        ' 设定数据记录当前位置讯息的初值
        TextBoxPosition.Text = String.Format("产品类别记录:当前位置 {0} 总数 {1}", bmCategory.Position + 1, bmCategory.Count)

        myConnection.Close()

    End Sub

    Protected Sub 产品类别_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBoxPosition.Text = String.Format("产品类别记录:当前位置 {0} 总数 {1}", bmCategory.Position + 1, bmCategory.Count)
    End Sub

    ' 按下 第一条 按钮
    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        ' 将 Position 属性设定成 0
        bmCategory.Position = 0
    End Sub

    ' 按下 上一条 按钮
    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        If bmCategory.Position > 0 Then
            ' 将 Position 属性递减 1
            bmCategory.Position -= 1
        End If
    End Sub

    ' 按下 下一条 按钮
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If bmCategory.Position < bmCategory.Count - 1 Then
            ' 将 Position 属性递增 1
            bmCategory.Position += 1
        End If
    End Sub

    ' 按下 最后一条 按钮
    Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
        bmCategory.Position = bmCategory.Count - 1
    End Sub
End Class

⌨️ 快捷键说明

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