VB.Net Tutorial/Database ADO.net/Data binding

Материал из VB Эксперт
Версия от 12:56, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Bind DataTable to Control

Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms
public class BindDataTableToControl
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    " The data source.
    Private m_ContactsTable As DataTable
    " The data source"s CurrencyManager.
    Private m_CurrencyManager As CurrencyManager
    Private Sub Form1_Load(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles MyBase.Load
        " Make a DataTable.
        m_ContactsTable = New DataTable("Contacts")
        " Add columns.
        m_ContactsTable.Columns.Add("FirstName", GetType(String))
        m_ContactsTable.Columns.Add("LastName", GetType(String))
        m_ContactsTable.Columns.Add("Street", GetType(String))
        m_ContactsTable.Columns.Add("City", GetType(String))
        m_ContactsTable.Columns.Add("State", GetType(String))
        m_ContactsTable.Columns.Add("Zip", GetType(String))
        " Make the combined FirstName/LastName unique.
        Dim first_last_columns() As DataColumn = { _
            m_ContactsTable.Columns("FirstName"), _
            m_ContactsTable.Columns("LastName") _
        }
        m_ContactsTable.Constraints.Add( _
            New UniqueConstraint(first_last_columns))
        " Make some contact data.
        m_ContactsTable.Rows.Add(New Object() {"A", "B","C", "D", "E", "11111"})
        m_ContactsTable.Rows.Add(New Object() {"F", "G","H", "I", "J", "22222"})
        m_ContactsTable.Rows.Add(New Object() {"K", "L","M", "N", "O", "33333"})
        m_ContactsTable.Rows.Add(New Object() {"P", "Q","R", "S", "T", "44444"})
        m_ContactsTable.Rows.Add(New Object() {"U", "V","W", "X", "Y", "55555"})
        m_ContactsTable.Rows.Add(New Object() {"Z", "A","B", "C", "D", "66666"})
        m_ContactsTable.Rows.Add(New Object() {"E", "F","G", "H", "I", "77777"})
        m_ContactsTable.Rows.Add(New Object() {"J", "K","L", "M", "N", "88888"})
        " Bind to controls.
        txtFirstName.DataBindings.Add("Text", m_ContactsTable, "FirstName")
        txtLastName.DataBindings.Add("Text", m_ContactsTable, "LastName")
        txtStreet.DataBindings.Add("Text", m_ContactsTable, "Street")
        txtCity.DataBindings.Add("Text", m_ContactsTable, "City")
        txtState.DataBindings.Add("Text", m_ContactsTable, "State")
        txtZip.DataBindings.Add("Text", m_ContactsTable, "Zip")
        " Save a reference to the CurrencyManager.
        m_CurrencyManager = _
            DirectCast(Me.BindingContext(m_ContactsTable), CurrencyManager)
    End Sub
    Private Sub btnFirst_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnFirst.Click
        m_CurrencyManager.Position = 0
    End Sub
    Private Sub btnPrev_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnPrev.Click
        m_CurrencyManager.Position -= 1
    End Sub
    Private Sub btnNext_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnNext.Click
        m_CurrencyManager.Position += 1
    End Sub
    Private Sub btnLast_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnLast.Click
        m_CurrencyManager.Position = m_CurrencyManager.Count - 1
    End Sub
    " Add a new record.
    Private Sub btnAdd_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnAdd.Click
        m_CurrencyManager.AddNew()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnDelete.Click
        If MsgBox("Are you sure you want to delete this record?", _
            MsgBoxStyle.Question Or MsgBoxStyle.YesNo, _
            "Confirm Delete?") = MsgBoxResult.Yes _
        Then
            m_CurrencyManager.RemoveAt(m_CurrencyManager.Position)
        End If
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Label6 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.btnDelete = New System.Windows.Forms.Button
        Me.btnAdd = New System.Windows.Forms.Button
        Me.btnLast = New System.Windows.Forms.Button
        Me.btnNext = New System.Windows.Forms.Button
        Me.btnPrev = New System.Windows.Forms.Button
        Me.btnFirst = New System.Windows.Forms.Button
        Me.txtZip = New System.Windows.Forms.TextBox
        Me.txtState = New System.Windows.Forms.TextBox
        Me.txtCity = New System.Windows.Forms.TextBox
        Me.txtStreet = New System.Windows.Forms.TextBox
        Me.txtLastName = New System.Windows.Forms.TextBox
        Me.txtFirstName = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        "
        "Label6
        "
        Me.Label6.AutoSize = True
        Me.Label6.Location = New System.Drawing.Point(176, 104)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(22, 13)
        Me.Label6.TabIndex = 35
        Me.Label6.Text = "Zip"
        "
        "Label5
        "
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(8, 104)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(32, 13)
        Me.Label5.TabIndex = 34
        Me.Label5.Text = "State"
        "
        "Label4
        "
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(8, 80)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(24, 13)
        Me.Label4.TabIndex = 33
        Me.Label4.Text = "City"
        "
        "Label3
        "
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(8, 56)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(35, 13)
        Me.Label3.TabIndex = 32
        Me.Label3.Text = "Street"
        "
        "Label2
        "
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(8, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(58, 13)
        Me.Label2.TabIndex = 31
        Me.Label2.Text = "Last Name"
        "
        "Label1
        "
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(57, 13)
        Me.Label1.TabIndex = 30
        Me.Label1.Text = "First Name"
        "
        "btnDelete
        "
        Me.btnDelete.Location = New System.Drawing.Point(240, 144)
        Me.btnDelete.Name = "btnDelete"
        Me.btnDelete.Size = New System.Drawing.Size(32, 24)
        Me.btnDelete.TabIndex = 29
        Me.btnDelete.Text = "X"
        "
        "btnAdd
        "
        Me.btnAdd.Location = New System.Drawing.Point(200, 144)
        Me.btnAdd.Name = "btnAdd"
        Me.btnAdd.Size = New System.Drawing.Size(32, 24)
        Me.btnAdd.TabIndex = 28
        Me.btnAdd.Text = "+"
        "
        "btnLast
        "
        Me.btnLast.Location = New System.Drawing.Point(104, 144)
        Me.btnLast.Name = "btnLast"
        Me.btnLast.Size = New System.Drawing.Size(32, 24)
        Me.btnLast.TabIndex = 27
        Me.btnLast.Text = ">>"
        "
        "btnNext
        "
        Me.btnNext.Location = New System.Drawing.Point(72, 144)
        Me.btnNext.Name = "btnNext"
        Me.btnNext.Size = New System.Drawing.Size(32, 24)
        Me.btnNext.TabIndex = 26
        Me.btnNext.Text = ">"
        "
        "btnPrev
        "
        Me.btnPrev.Location = New System.Drawing.Point(40, 144)
        Me.btnPrev.Name = "btnPrev"
        Me.btnPrev.Size = New System.Drawing.Size(32, 24)
        Me.btnPrev.TabIndex = 25
        Me.btnPrev.Text = "<"
        "
        "btnFirst
        "
        Me.btnFirst.Location = New System.Drawing.Point(8, 144)
        Me.btnFirst.Name = "btnFirst"
        Me.btnFirst.Size = New System.Drawing.Size(32, 24)
        Me.btnFirst.TabIndex = 24
        Me.btnFirst.Text = "<<"
        "
        "txtZip
        "
        Me.txtZip.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtZip.Location = New System.Drawing.Point(200, 104)
        Me.txtZip.Name = "txtZip"
        Me.txtZip.Size = New System.Drawing.Size(72, 20)
        Me.txtZip.TabIndex = 23
        "
        "txtState
        "
        Me.txtState.Location = New System.Drawing.Point(72, 104)
        Me.txtState.Name = "txtState"
        Me.txtState.Size = New System.Drawing.Size(32, 20)
        Me.txtState.TabIndex = 22
        "
        "txtCity
        "
        Me.txtCity.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtCity.Location = New System.Drawing.Point(72, 80)
        Me.txtCity.Name = "txtCity"
        Me.txtCity.Size = New System.Drawing.Size(200, 20)
        Me.txtCity.TabIndex = 21
        "
        "txtStreet
        "
        Me.txtStreet.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtStreet.Location = New System.Drawing.Point(72, 56)
        Me.txtStreet.Name = "txtStreet"
        Me.txtStreet.Size = New System.Drawing.Size(200, 20)
        Me.txtStreet.TabIndex = 20
        "
        "txtLastName
        "
        Me.txtLastName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtLastName.Location = New System.Drawing.Point(72, 32)
        Me.txtLastName.Name = "txtLastName"
        Me.txtLastName.Size = New System.Drawing.Size(200, 20)
        Me.txtLastName.TabIndex = 19
        "
        "txtFirstName
        "
        Me.txtFirstName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtFirstName.Location = New System.Drawing.Point(72, 8)
        Me.txtFirstName.Name = "txtFirstName"
        Me.txtFirstName.Size = New System.Drawing.Size(200, 20)
        Me.txtFirstName.TabIndex = 18
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(278, 175)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.btnDelete)
        Me.Controls.Add(Me.btnAdd)
        Me.Controls.Add(Me.btnLast)
        Me.Controls.Add(Me.btnNext)
        Me.Controls.Add(Me.btnPrev)
        Me.Controls.Add(Me.btnFirst)
        Me.Controls.Add(Me.txtZip)
        Me.Controls.Add(Me.txtState)
        Me.Controls.Add(Me.txtCity)
        Me.Controls.Add(Me.txtStreet)
        Me.Controls.Add(Me.txtLastName)
        Me.Controls.Add(Me.txtFirstName)
        Me.Name = "Form1"
        Me.Text = "UseCurrencyManager"
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents btnDelete As System.Windows.Forms.Button
    Friend WithEvents btnAdd As System.Windows.Forms.Button
    Friend WithEvents btnLast As System.Windows.Forms.Button
    Friend WithEvents btnNext As System.Windows.Forms.Button
    Friend WithEvents btnPrev As System.Windows.Forms.Button
    Friend WithEvents btnFirst As System.Windows.Forms.Button
    Friend WithEvents txtZip As System.Windows.Forms.TextBox
    Friend WithEvents txtState As System.Windows.Forms.TextBox
    Friend WithEvents txtCity As System.Windows.Forms.TextBox
    Friend WithEvents txtStreet As System.Windows.Forms.TextBox
    Friend WithEvents txtLastName As System.Windows.Forms.TextBox
    Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
End Class

Data binding

Public Class Form2
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    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
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.Container
    Private WithEvents textBox1 As System.Windows.Forms.TextBox
    Private WithEvents button1 As System.Windows.Forms.Button
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.textBox1 = New System.Windows.Forms.TextBox()
        Me.button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        "
        "textBox1
        "
        Me.textBox1.Location = New System.Drawing.Point(48, 56)
        Me.textBox1.Name = "textBox1"
        Me.textBox1.TabIndex = 0
        Me.textBox1.Text = "textBox1"
        "
        "button1
        "
        Me.button1.Location = New System.Drawing.Point(56, 96)
        Me.button1.Name = "button1"
        Me.button1.TabIndex = 1
        Me.button1.Text = "button1"
        "
        "Form2
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button1, Me.textBox1})
        Me.Name = "Form2"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
        Dim sConnection As String = _
        "Provider=SQLOLEDB.1;" & _
        "Password=sqlpassword;" & _
        "Persist Security Info=True;" & _
        "User ID=sa;" & _
        "Initial Catalog=CD;" & _
        "Data Source=(local)"
        Dim sSQL As String
        sSQL = "SELECT ArtistID, ArtistName From Artist"
        Dim objConn _
         As New OleDb.OleDbConnection(sConnection)
        Dim objDataAdapter _
          As New OleDb.OleDbDataAdapter(sSQL, objConn)
        Dim objDS _
          As New DataSet("Artists")
        Dim objDV _
          As DataView
        Try
            objConn.Open()
        Catch myException As System.Exception
            Windows.Forms.MessageBox.Show(myException.Message)
        End Try
        If objConn.State = ConnectionState.Open Then
            Try
                objDataAdapter.Fill(objDS, "Disc")
                objConn.Close()
                Dim objTable As DataTable
                objTable = objDS.Tables("Disc")
                objDV = objTable.DefaultView
                textBox1.DataBindings.Add("Text", objDV, "ArtistName")
            Catch myexception As Exception
                Windows.Forms.MessageBox.Show(myException.Message)
            End Try
        End If
    End Sub
End Class