Step 1: Declare the DataAdapter and BindingSource with “form level” Scope.
Public Class Form1
Dim DGV1_DataAdapter As New OleDbDataAdapter
Dim DGV1_BindingSource1 As New BindingSource
Step 2: Load the BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DGVLoadBindingSourceWithSQLResults_OLEDB(DataGridView1, "Select * From Snippets", DGV1_DataAdapter, DGV1_BindingSource1)
Public Sub DGVLoadBindingSourceWithSQLResults_OLEDB(ByRef dgv As Windows.Forms.DataGridView, ByVal selectCommand As String, ByRef da As OleDbDataAdapter, ByRef bs As BindingSource)
Try
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Snippets.mdb"
' Create a new data adapter based on the specified query.
da = New OleDbDataAdapter(selectCommand, ConnectionString)
' Create a command builder to generate SQL update, insert, and
' delete commands based on selectCommand. These are used to
' update the database.
Dim commandBuilder As New OleDbCommandBuilder(da)
' Populate a new data table and bind it to the BindingSource.
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
da.Fill(table)
bs.DataSource = table
dgv.DataSource = bs
Catch ex As OleDbException
MessageBox.Show(ex.Message)
End Try
End Sub
Step 3: Use the functions found in the DataGridViewFunctions.vb file
Click Here for DataGridViewFunctions.vb
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
DGVUpdateDatabase(DGV1_DataAdapter, DGV1_BindingSource1)
MessageBox.Show("Records Updated.")
End Sub
Private Sub cmdQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdQuery.Click
DGVLoadBindingSourceWithSQLResults_OLEDB(DataGridView1, "Select * From Snippets where description like '%java%'", DGV1_DataAdapter, DGV1_BindingSource1)
End Sub
Private Sub cmdMoveLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMoveLast.Click
DGVMoveLast(DGV1_BindingSource1)
End Sub
Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFind.Click
DGVFind(DGV1_BindingSource1, "EntryDate", "7/9/2005")
End Sub