Data Grid Control in VB.Net
Data Grid Control is used to show the data in tabular form. This article is also easily demonstrate that how to connect with a database.
Data Grid Control is used to show the data in tabular form. This article is also easily demonstrate that how to connect with a database.
Please consider following :
Database used : "Microsoft Office Access 2007 (.accdb)"
Database Name : "Employee.accdb"
Code :
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
' Connecting to the Employee.accdb Database
Dim Con As OleDbConnection = NewOleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\Employee.accdb")
' Create a Data
Dim Dad As OleDbDataAdapter = New OleDbDataAdapter("Select * from Table1", Con)
' Create a Data Set
Dim Dst As DataSet = New DataSet
' Fill the Database
Dad.Fill(Dst, "Employee")
' Attach DataSet to DataGrid
DataGrid.DataSource = Dst.Tables(0)
' Code to count and show the field in "Table1" Table
Con.Open()
Dim Cmd As New System.Data.OleDb.OleDbCommand()
Cmd.Connection = Con
Cmd.CommandText = "Select * from Table1"
Dim Drd As OleDbDataReader
Drd = Cmd.ExecuteReader
Label1.Text = "Number of Fields in Table1 :: " & Drd.FieldCount.ToString
Drd.Close()
Con.Close()
End Sub
End Class
Output :
