VB.Net/Database ADO.net/Data Bind to DataGrid

Материал из VB Эксперт
Перейти к: навигация, поиск

Data binding to a DataGrid

Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Data.SqlClient
public class MainClass
   Shared Sub Main()
       Dim form1 As Form = New Form1
       Application.Run(form1)
   End Sub
End Class

Public Class Form1
   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.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.
   Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.DataGrid1 = New System.Windows.Forms.DataGrid
      CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).BeginInit()
      Me.SuspendLayout()
      "
      "DataGrid1
      "
      Me.DataGrid1.DataMember = ""
      Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
      Me.DataGrid1.Location = New System.Drawing.Point(8, 0)
      Me.DataGrid1.Name = "DataGrid1"
      Me.DataGrid1.Size = New System.Drawing.Size(552, 280)
      Me.DataGrid1.TabIndex = 0
      "
      "Form1
      "
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(568, 285)
      Me.Controls.Add(Me.DataGrid1)
      Me.Name = "Form1"
      Me.Text = "Form1"
      CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).EndInit()
      Me.ResumeLayout(False)
   End Sub
#End Region
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      "Create Connection object
      Dim thisConnection As New SqlConnection _
         ("server=(local)\SQLEXPRESS;" & _
          "integrated security=sspi;" & _
          "database=MyDatabase")
      " Sql Query 
      Dim sql As String = _
         "SELECT * FROM Employee"
      " Create Data Adapter
      Dim da As New SqlDataAdapter(sql, thisConnection)
      " Create and fill Dataset
      Dim ds As New DataSet
      da.Fill(ds, "Employee")
      " Bind the data table to the data grid
      DataGrid1.SetDataBinding(ds, "Employee")
      
   End Sub
End Class


Data binding to Data Grid

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources
Public Class MainClass
    Shared Sub Main()
       Dim form1 As Form = new FrmTableDisplay()
       Application.Run(form1)
    End Sub
End Class
Public Class FrmTableDisplay
   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
      " fill DataSet1 with data
      OleDbDataAdapter1.Fill(DataSet1, "Employee")
      " bind data in Users table in DataSet1 to dataGrid1
      dgdEmployee.SetDataBinding(DataSet1, "Employee")
   End Sub " New
   " 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 " Dispose
   Friend WithEvents dgdEmployee As System.Windows.Forms.DataGrid
   Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
   Friend WithEvents OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand
   Friend WithEvents OleDbUpdateCommand1 As System.Data.OleDb.OleDbCommand
   Friend WithEvents OleDbDeleteCommand1 As System.Data.OleDb.OleDbCommand
   Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
   Friend WithEvents OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter
   Friend WithEvents DataSet1 As System.Data.DataSet
   " Required by the Windows Form Designer
   Private components As System.ruponentModel.Container
   " 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.dgdEmployee = New System.Windows.Forms.DataGrid()
      Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand()
      Me.OleDbInsertCommand1 = New System.Data.OleDb.OleDbCommand()
      Me.OleDbUpdateCommand1 = New System.Data.OleDb.OleDbCommand()
      Me.OleDbDeleteCommand1 = New System.Data.OleDb.OleDbCommand()
      Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection()
      Me.OleDbDataAdapter1 = New System.Data.OleDb.OleDbDataAdapter()
      Me.DataSet1 = New System.Data.DataSet()
      CType(Me.dgdEmployee,System.ruponentModel.ISupportInitialize).BeginInit()
      CType(Me.DataSet1, System.ruponentModel.ISupportInitialize).BeginInit()
      Me.SuspendLayout()
      "
      " dgdEmployee
      "
      Me.dgdEmployee.DataMember = ""
      Me.dgdEmployee.Location = New System.Drawing.Point(8, 8)
      Me.dgdEmployee.Name = "dgdEmployee"
      Me.dgdEmployee.Size = New System.Drawing.Size(304, 256)
      Me.dgdEmployee.TabIndex = 0
      "
      " OleDbSelectCommand1
      "
      Me.OleDbSelectCommand1.rumandText = "SELECT ID, FirstName, LastName FROM Employee"
      Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
      "
      " OleDbInsertCommand1
      "
      Me.OleDbInsertCommand1.rumandText = "INSERT INTO Employee(ID, FirstName, LastName)" & _
         "VALUES (?, ?, ?)"
      Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1
      Me.OleDbInsertCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter("ID", _
         System.Data.OleDb.OleDbType.Numeric, 0, _
         System.Data.ParameterDirection.Input, False, _
         CType(10, Byte), CType(0, Byte), "ID", _
         System.Data.DataRowVersion.Current, Nothing))
      Me.OleDbInsertCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter("FirstName", _
         System.Data.OleDb.OleDbType.Char, 50, _
         System.Data.ParameterDirection.Input, False, _
         CType(0, Byte), CType(0, Byte), "FirstName", _
         System.Data.DataRowVersion.Current, Nothing))
      Me.OleDbInsertCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter("LastName", _
         System.Data.OleDb.OleDbType.Char, 50, _
         System.Data.ParameterDirection.Input, False, _
         CType(0, Byte), CType(0, Byte), "LastName", _
         System.Data.DataRowVersion.Current, Nothing))
      "
      " OleDbUpdateCommand1
      "
      Me.OleDbUpdateCommand1.rumandText = _
         "UPDATE Employee SET ID = ?, FirstName = ?, " & _
         "LastName = ? WHERE (ID = ?)" & _
         " AND (FirstName = ?) AND (LastName = ?)"
      Me.OleDbUpdateCommand1.Connection = Me.OleDbConnection1
      Me.OleDbUpdateCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter("ID", _
         System.Data.OleDb.OleDbType.Numeric, 0, _
         System.Data.ParameterDirection.Input, False, _
         CType(10, Byte), CType(0, Byte), "ID", _
         System.Data.DataRowVersion.Current, Nothing))
      Me.OleDbUpdateCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter("FirstName", _
         System.Data.OleDb.OleDbType.Char, 50, _
         System.Data.ParameterDirection.Input, False, _
         CType(0, Byte), CType(0, Byte), "FirstName", _
         System.Data.DataRowVersion.Current, Nothing))
      Me.OleDbUpdateCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter("LastName", _
         System.Data.OleDb.OleDbType.Char, 50, _
         System.Data.ParameterDirection.Input, False, _
         CType(0, Byte), CType(0, Byte), "LastName", _
         System.Data.DataRowVersion.Current, Nothing))
      Me.OleDbUpdateCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter _
         ("Original_ID", _
         System.Data.OleDb.OleDbType.Numeric, 0, _
         System.Data.ParameterDirection.Input, False, _
         CType(10, Byte), CType(0, Byte), "ID", _
         System.Data.DataRowVersion.Original, Nothing))
      Me.OleDbUpdateCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter _
         ("Original_FirstName", _
         System.Data.OleDb.OleDbType.Char, 50, _
         System.Data.ParameterDirection.Input, False, _
         CType(0, Byte), CType(0, Byte), "FirstName", _
         System.Data.DataRowVersion.Original, Nothing))
      Me.OleDbUpdateCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter _
         ("Original_LastName", _
         System.Data.OleDb.OleDbType.Char, 50, _
         System.Data.ParameterDirection.Input, False, _
         CType(0, Byte), CType(0, Byte), "LastName", _
         System.Data.DataRowVersion.Original, Nothing))
      "
      " OleDbDeleteCommand1
      "
      Me.OleDbDeleteCommand1.rumandText = _
         "DELETE FROM Employee WHERE (ID = ?) AND " & _
         "(FirstName = ?) AND (LastName = ?)"
      Me.OleDbDeleteCommand1.Connection = Me.OleDbConnection1
      Me.OleDbDeleteCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter("ID", _
         System.Data.OleDb.OleDbType.Numeric, 0, _
         System.Data.ParameterDirection.Input, False, _
         CType(10, Byte), CType(0, Byte), "ID", _
         System.Data.DataRowVersion.Original, Nothing))
      Me.OleDbDeleteCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter("FirstName", _
         System.Data.OleDb.OleDbType.Char, 50, _
         System.Data.ParameterDirection.Input, False, _
         CType(0, Byte), CType(0, Byte), "FirstName", _
         System.Data.DataRowVersion.Original, Nothing))
      Me.OleDbDeleteCommand1.Parameters.Add _
         (New System.Data.OleDb.OleDbParameter("LastName", _
         System.Data.OleDb.OleDbType.Char, 50, _
         System.Data.ParameterDirection.Input, False, _
         CType(0, Byte), CType(0, Byte), "LastName", _
         System.Data.DataRowVersion.Original, Nothing))
      "
      "OleDbConnection1
      "
      Me.OleDbConnection1.ConnectionString = _
         "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";" & _
         "User ID=Admin;Data Source=Employee.mdb;Mode=Sha" & _
         "re Deny None;Extended Properties="""";" & _
         "Jet OLEDB:System database="""";Jet OLEDB:Regis" & _
         "try Path="""";Jet OLEDB:Database Password="""";" & _
         "Jet OLEDB:Engine Type=5;Jet OLEDB:Dat" & _
         "abase Locking Mode=1;Jet OLEDB:Global Partial " & _
         "Bulk Ops=2;Jet OLEDB:Global Bulk T" & _
         "ransactions=1;Jet OLEDB:New Database " & _
         "Password="""";Jet OLEDB:Create System Databas" & _
         "e=False;Jet OLEDB:Encrypt Database=False;" & _
         "Jet OLEDB:Don"t Copy Locale on Compact=" & _
         "False;Jet OLEDB:Compact Without Replica " & _
         "Repair=False;Jet OLEDB:SFP=False"
      "
      " OleDbDataAdapter1
      "
      Me.OleDbDataAdapter1.DeleteCommand = _
         Me.OleDbDeleteCommand1
      Me.OleDbDataAdapter1.InsertCommand = _
         Me.OleDbInsertCommand1
      Me.OleDbDataAdapter1.SelectCommand = _
              Me.OleDbSelectCommand1
      Me.OleDbDataAdapter1.TableMappings.AddRange _
         (New System.Data.rumon.DataTableMapping() _
         {New System.Data.rumon.DataTableMapping("Table", _
         "Employee", New System.Data.rumon.DataColumnMapping() _
         {New System.Data.rumon.DataColumnMapping("ID", _
         "ID"), New System.Data.rumon.DataColumnMapping _
         ("FirstName", "FirstName"), _
         New System.Data.rumon.DataColumnMapping("LastName", _
         "LastName")})})
      Me.OleDbDataAdapter1.UpdateCommand = _
         Me.OleDbUpdateCommand1
      "
      " DataSet1
      "
      Me.DataSet1.DataSetName = "NewDataSet"
      Me.DataSet1.Locale = _
         New System.Globalization.CultureInfo("en-US")
      "
      " FrmTableDisplay
      "
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(320, 273)
      Me.Controls.AddRange(New System.Windows.Forms.Control() _
         {Me.dgdEmployee})
      Me.Name = "FrmTableDisplay"
      Me.Text = "Table Display"
      CType(Me.dgdEmployee, System.ruponentModel. _
         ISupportInitialize).EndInit()
      CType(Me.DataSet1, System.ruponentModel. _
         ISupportInitialize).EndInit()
      Me.ResumeLayout(False)
   End Sub " InitializeComponent
#End Region
End Class

<A href="http://www.vbex.ru/Code/VBDownload/Employee.zip">Employee.zip( 7 k)</a>


Use Data Grid to update Data

Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Data.SqlClient
public class MainClass
   Shared Sub Main()
       Dim form1 As Form = New Form1
       Application.Run(form1)
   End Sub
End Class

Public Class Form1
   Inherits System.Windows.Forms.Form
   Private cb As SqlCommandBuilder
   Private da As SqlDataAdapter
#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.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.
   Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
   Friend WithEvents buttonUpdate As System.Windows.Forms.Button
   Friend WithEvents DataSet1 As System.Data.DataSet
   Friend WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.DataGrid1 = New System.Windows.Forms.DataGrid
      Me.buttonUpdate = New System.Windows.Forms.Button
      Me.DataSet1 = New System.Data.DataSet
      Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand
      CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).BeginInit()
      CType(Me.DataSet1, System.ruponentModel.ISupportInitialize).BeginInit()
      Me.SuspendLayout()
      "
      "DataGrid1
      "
      Me.DataGrid1.DataMember = ""
      Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
      Me.DataGrid1.Location = New System.Drawing.Point(8, 8)
      Me.DataGrid1.Name = "DataGrid1"
      Me.DataGrid1.Size = New System.Drawing.Size(400, 192)
      Me.DataGrid1.TabIndex = 0
      "
      "buttonUpdate
      "
      Me.buttonUpdate.Location = New System.Drawing.Point(171, 208)
      Me.buttonUpdate.Name = "buttonUpdate"
      Me.buttonUpdate.TabIndex = 1
      Me.buttonUpdate.Text = "Update"
      "
      "DataSet1
      "
      Me.DataSet1.DataSetName = "NewDataSet"
      Me.DataSet1.Locale = New System.Globalization.CultureInfo("en-GB")
      "
      "Form1
      "
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(416, 245)
      Me.Controls.Add(Me.buttonUpdate)
      Me.Controls.Add(Me.DataGrid1)
      Me.Name = "Form1"
      Me.Text = "Form1"
      CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).EndInit()
      CType(Me.DataSet1, System.ruponentModel.ISupportInitialize).EndInit()
      Me.ResumeLayout(False)
   End Sub
#End Region
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      "Create Connection object
      Dim thisConnection As New SqlConnection _
         ("server=(local)\SQLEXPRESS;" & _
          "integrated security=sspi;" & _
          "database=MyDatabase")
      " Sql Query
      Dim sql As String = _
         "SELECT * FROM Employee"
      " Create a Command
      SqlCommand1 = New SqlCommand(sql, thisConnection)
      " Create SqlDataAdapter
      da = New SqlDataAdapter
      da.SelectCommand = SqlCommand1
      " Create SqlCommandBuilder object
      cb = New SqlCommandBuilder(da)
      " Fill Dataset
      da.Fill(DataSet1, "Employee")
      " Bind the data to the grid at runtime
      DataGrid1.SetDataBinding(DataSet1, "Employee")
   End Sub
   Private Sub buttonUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonUpdate.Click
      da.Update(DataSet1, "Employee")
   End Sub
End Class