To show database in datagridview
hi guys,
i have the following code,
VB Code:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim objconnection As OleDbConnection
Dim datagridview1 As New DataGridView
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim form2 As New Form2
form2.Show()
objconnection = New OleDbConnection( _
"provider=microsoft.jet.oledb.4.0;" & _
"data source =" & TextBox1.Text & ";")
Try
objconnection.Open()
Catch oledbexceptionerr As OleDbException
Debug.WriteLine(oledbexceptionerr.Message)
Catch invalidoperationerr As InvalidOperationException
Debug.WriteLine(invalidoperationerr.Message)
End Try
Dim objcommand As New OleDbCommand("SELECT * from Products", objconnection)
Dim objDataAdapter As New OleDbDataAdapter
Dim objDataTable As New DataTable
objDataAdapter.SelectCommand = objcommand
Dim dataset As New DataSet
Try
objDataAdapter.Fill(objDataTable)
datagridview1.DataSource = objDataTable
datagridview1.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke
datagridview1.CellBorderStyle = DataGridViewCellBorderStyle.None
datagridview1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Catch OleDbExceptionErr As OleDbException
MessageBox.Show(OleDbExceptionErr.Message)
End Try
objcommand.Dispose()
objcommand = Nothing
objDataAdapter.Dispose()
objDataAdapter = Nothing
objDataTable.Dispose()
objDataTable = Nothing
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ofd As New OpenFileDialog
With ofd
.Filter = "mdb|*.mdb"
.FilterIndex = 1
.Title = "opendatabase"
End With
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = ofd.FileName
End If
End Sub
End Class
I have 2 buttons and a textbox in first form and in other form i have a datagrid. i need to show the database in datagrid when i click the button. i tried with the above code but nothing is being displayed in the datagrid.