Load data from database into DataGrid : DataGrid « Database ADO.net « VB.Net

Load data from database into DataGrid

Load data from database into DataGrid
 Imports System
 Imports System.Drawing
 Imports System.Collections
 Imports System.ComponentModel
 Imports System.Windows.Forms
 Imports System.Data
 Imports System.Data.SqlClient

Public Class MainClass

    Shared Sub Main(  )
        Application.Run(New ADOForm1() )
    End Sub

End Class

  Public Class ADOForm1
     Inherits System.Windows.Forms.Form

     Private components As System.ComponentModel.Container
     Friend WithEvents EmployeeDataGrid As _

     Public Sub New(  )
         InitializeComponent(  )

         Dim connectionString As String ="server=(local)\SQLEXPRESS;" & _
          "integrated security=sspi;database=MyDatabase"

         Dim commandString As String = _
           "Select FirstName, LastName from Employee"

         Dim myDataAdapter As _
         New SqlDataAdapter(commandString, connectionString)
         Dim myDataSet As New DataSet(  )
         myDataAdapter.Fill(myDataSet, "Employee")

         ' bind the DataSet to the grid
         EmployeeDataGrid.DataSource = _

     End Sub 'New

     Private Sub InitializeComponent(  )
         Me.components = New System.ComponentModel.Container(  )
         Me.EmployeeDataGrid = New System.Windows.Forms.DataGrid(  )
         EmployeeDataGrid.Location = New System.Drawing.Point(48, 24)
         EmployeeDataGrid.Size = New System.Drawing.Size(368, 160)
         EmployeeDataGrid.TabIndex = 0
         Me.Text = "ADOFrm1"
         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
         Me.ClientSize = New System.Drawing.Size(464, 273)
     End Sub 

 End Class 

Related examples in the same category

1.DataGrid: Load data table with data readerDataGrid: Load data table with data reader
2.Click button to Update DataGridClick button to Update DataGrid
3.Load result set from 'select' command into a DataGridLoad result set from 'select' command into a DataGrid
4.Use datagrid to display table schemaUse datagrid to display table schema
5.Bind data into a data grid and do the updateBind data into a data grid and do the update