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

📄 bindingdemoform7.vb

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

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

    ' CurrencyManager 对象的类层级声明
    Dim bmEmployeeHealth 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 lblHeader As System.Windows.Forms.Label
    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 DateTimePickerBirthday As System.Windows.Forms.DateTimePicker
    Friend WithEvents CheckBoxGender As System.Windows.Forms.CheckBox
    Friend WithEvents lblBirthday As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents lblName As System.Windows.Forms.Label
    Friend WithEvents lblID As System.Windows.Forms.Label
    Friend WithEvents TextBoxName As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxID As System.Windows.Forms.TextBox
    Friend WithEvents lblBloodType As System.Windows.Forms.Label
    Friend WithEvents ComboBoxBloodType As System.Windows.Forms.ComboBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lblHeader = New System.Windows.Forms.Label()
        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.DateTimePickerBirthday = New System.Windows.Forms.DateTimePicker()
        Me.CheckBoxGender = New System.Windows.Forms.CheckBox()
        Me.lblBloodType = New System.Windows.Forms.Label()
        Me.lblBirthday = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.lblName = New System.Windows.Forms.Label()
        Me.lblID = New System.Windows.Forms.Label()
        Me.TextBoxName = New System.Windows.Forms.TextBox()
        Me.TextBoxID = New System.Windows.Forms.TextBox()
        Me.ComboBoxBloodType = New System.Windows.Forms.ComboBox()
        Me.SuspendLayout()
        '
        'lblHeader
        '
        Me.lblHeader.AutoSize = True
        Me.lblHeader.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblHeader.ForeColor = System.Drawing.Color.Red
        Me.lblHeader.Location = New System.Drawing.Point(93, 17)
        Me.lblHeader.Name = "lblHeader"
        Me.lblHeader.Size = New System.Drawing.Size(313, 24)
        Me.lblHeader.TabIndex = 59
        Me.lblHeader.Text = "使用 ComboBox 控件来编辑数据"
        '
        'btnEnd
        '
        Me.btnEnd.Location = New System.Drawing.Point(338, 374)
        Me.btnEnd.Name = "btnEnd"
        Me.btnEnd.Size = New System.Drawing.Size(75, 27)
        Me.btnEnd.TabIndex = 58
        Me.btnEnd.Text = "最后一条"
        '
        'btnNext
        '
        Me.btnNext.Location = New System.Drawing.Point(249, 374)
        Me.btnNext.Name = "btnNext"
        Me.btnNext.Size = New System.Drawing.Size(75, 27)
        Me.btnNext.TabIndex = 57
        Me.btnNext.Text = "下一条"
        '
        'btnBack
        '
        Me.btnBack.Location = New System.Drawing.Point(160, 374)
        Me.btnBack.Name = "btnBack"
        Me.btnBack.Size = New System.Drawing.Size(75, 27)
        Me.btnBack.TabIndex = 56
        Me.btnBack.Text = "上一条"
        '
        'btnFirst
        '
        Me.btnFirst.Location = New System.Drawing.Point(71, 374)
        Me.btnFirst.Name = "btnFirst"
        Me.btnFirst.Size = New System.Drawing.Size(75, 27)
        Me.btnFirst.TabIndex = 55
        Me.btnFirst.Text = "第一条"
        '
        '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(77, 329)
        Me.TextBoxPosition.Name = "TextBoxPosition"
        Me.TextBoxPosition.Size = New System.Drawing.Size(333, 25)
        Me.TextBoxPosition.TabIndex = 54
        Me.TextBoxPosition.Text = ""
        Me.TextBoxPosition.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'DateTimePickerBirthday
        '
        Me.DateTimePickerBirthday.Location = New System.Drawing.Point(131, 155)
        Me.DateTimePickerBirthday.Name = "DateTimePickerBirthday"
        Me.DateTimePickerBirthday.TabIndex = 47
        '
        'CheckBoxGender
        '
        Me.CheckBoxGender.Location = New System.Drawing.Point(131, 125)
        Me.CheckBoxGender.Name = "CheckBoxGender"
        Me.CheckBoxGender.Size = New System.Drawing.Size(16, 24)
        Me.CheckBoxGender.TabIndex = 43
        '
        'lblBloodType
        '
        Me.lblBloodType.AutoSize = True
        Me.lblBloodType.Location = New System.Drawing.Point(82, 192)
        Me.lblBloodType.Name = "lblBloodType"
        Me.lblBloodType.Size = New System.Drawing.Size(40, 18)
        Me.lblBloodType.TabIndex = 50
        Me.lblBloodType.Text = "血型:"
        '
        'lblBirthday
        '
        Me.lblBirthday.AutoSize = True
        Me.lblBirthday.Location = New System.Drawing.Point(51, 158)
        Me.lblBirthday.Name = "lblBirthday"
        Me.lblBirthday.Size = New System.Drawing.Size(71, 18)
        Me.lblBirthday.TabIndex = 46
        Me.lblBirthday.Text = "出生日期:"
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(82, 128)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(40, 18)
        Me.Label1.TabIndex = 42
        Me.Label1.Text = "性别:"
        '
        'lblName
        '
        Me.lblName.AutoSize = True
        Me.lblName.Location = New System.Drawing.Point(82, 99)
        Me.lblName.Name = "lblName"
        Me.lblName.Size = New System.Drawing.Size(40, 18)
        Me.lblName.TabIndex = 40
        Me.lblName.Text = "姓名:"
        '
        'lblID
        '
        Me.lblID.AutoSize = True
        Me.lblID.Location = New System.Drawing.Point(51, 64)
        Me.lblID.Name = "lblID"
        Me.lblID.Size = New System.Drawing.Size(71, 18)
        Me.lblID.TabIndex = 38
        Me.lblID.Text = "员工编号:"
        '
        'TextBoxName
        '
        Me.TextBoxName.Location = New System.Drawing.Point(131, 96)
        Me.TextBoxName.Name = "TextBoxName"
        Me.TextBoxName.TabIndex = 41
        Me.TextBoxName.Text = ""
        '
        'TextBoxID
        '
        Me.TextBoxID.Location = New System.Drawing.Point(131, 61)
        Me.TextBoxID.Name = "TextBoxID"
        Me.TextBoxID.TabIndex = 39
        Me.TextBoxID.Text = ""
        '
        'ComboBoxBloodType
        '
        Me.ComboBoxBloodType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.ComboBoxBloodType.Location = New System.Drawing.Point(131, 190)
        Me.ComboBoxBloodType.Name = "ComboBoxBloodType"
        Me.ComboBoxBloodType.Size = New System.Drawing.Size(320, 23)
        Me.ComboBoxBloodType.TabIndex = 60
        '
        'BindingDemoForm7
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 18)
        Me.ClientSize = New System.Drawing.Size(474, 417)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ComboBoxBloodType, Me.lblHeader, Me.btnEnd, Me.btnNext, Me.btnBack, Me.btnFirst, Me.TextBoxPosition, Me.DateTimePickerBirthday, Me.CheckBoxGender, Me.lblBloodType, Me.lblBirthday, Me.Label1, Me.lblName, Me.lblID, Me.TextBoxName, Me.TextBoxID})
        Me.Name = "BindingDemoForm7"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "ComboBox 控件---复杂的数据绑定"
        Me.ResumeLayout(False)

    End Sub

#End Region

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

        ' 建立一个连接字符串
        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 Type, Characteristic FROM BloodType"

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

        ' 将 TextBox 控件的 Text 属性绑定至数据集 ds 内的 人员健康 数据表的 员工编号 字段
        TextBoxID.DataBindings.Add("Text", ds, "人员健康.员工编号")
        ' 将 TextBox 控件的 Text 属性绑定至数据集 ds 内的 人员健康 数据表的 姓名 字段
        TextBoxName.DataBindings.Add("Text", ds, "人员健康.姓名")
        ' 将 CheckBox 控件的 Checked 属性绑定至数据集 ds 内的 人员健康 数据表的 性别 字段
        CheckBoxGender.DataBindings.Add("Checked", ds, "人员健康.性别")
        ' 将 DateTimePicker 控件的 Value 属性绑定至数据集 ds 内的 人员健康 数据表的 出生日期 字段
        DateTimePickerBirthday.DataBindings.Add("Value", ds, "人员健康.出生日期")

        ' 将 ComboBox 控件的数据源设定成数据集 ds 内的 BloodType 数据表
        ComboBoxBloodType.DataSource = ds.Tables("BloodType")
        ' 将 ComboBox 控件的 DisplayMember 属性设定成 BloodType 数据表的 Characteristic 字段
        ComboBoxBloodType.DisplayMember = "Characteristic"
        ' 将 ComboBox 控件的 ValueMember 属性设定成 BloodType 数据表的 Type 字段
        ComboBoxBloodType.ValueMember = "Type"

        ' 将 SelectedValue 属性绑定至 人员健康 数据表的 血型 字段,如这一来,
        ' 用户所选择的选项的 Type 字段内容便会写入 人员健康 数据表的 血型 字段。
        ComboBoxBloodType.DataBindings.Add("SelectedValue", ds, "人员健康.血型")

        ' 取得代表 人员健康 数据表的 CurrencyManager 对象
        bmEmployeeHealth = Me.BindingContext(ds, "人员健康")

        ' 设定当引发 PositionChanged 事件时便执行事件处理例程 人员健康_PositionChanged
        AddHandler bmEmployeeHealth.PositionChanged, AddressOf 人员健康_PositionChanged

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

        ' 关闭对数据库的连接
        myConnection.Close()
    End Sub
    ' 更新数据记录当前位置的讯息
    Protected Sub 人员健康_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBoxPosition.Text = String.Format("数据记录:当前位置 {0} 总数 {1}", bmEmployeeHealth.Position + 1, bmEmployeeHealth.Count)
    End Sub

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

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

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

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

⌨️ 快捷键说明

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