Data binding to Data Grid : Data Bind to DataGrid « Database ADO.net « VB.Net






Data binding to Data Grid

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.ComponentModel.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.ComponentModel.ISupportInitialize).BeginInit()

      CType(Me.DataSet1, System.ComponentModel.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.CommandText = "SELECT ID, FirstName, LastName FROM Employee"

      Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1

      '
      ' OleDbInsertCommand1
      '
      Me.OleDbInsertCommand1.CommandText = "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.CommandText = _
         "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.CommandText = _
         "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.Common.DataTableMapping() _
         {New System.Data.Common.DataTableMapping("Table", _
         "Employee", New System.Data.Common.DataColumnMapping() _
         {New System.Data.Common.DataColumnMapping("ID", _
         "ID"), New System.Data.Common.DataColumnMapping _
         ("FirstName", "FirstName"), _
         New System.Data.Common.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.ComponentModel. _
         ISupportInitialize).EndInit()

      CType(Me.DataSet1, System.ComponentModel. _
         ISupportInitialize).EndInit()

      Me.ResumeLayout(False)

   End Sub ' InitializeComponent

#End Region

End Class 
           
       








Employee.zip( 7 k)

Related examples in the same category

1.Data binding to a DataGrid
2.Use Data Grid to update DataUse Data Grid to update Data