[2008] Data Grid View with Access
Good Evening, I am learning DB programming at the moment so all new to it and I have learnt how to connect to a database insert new things delete read a line etc but there is something new I have come across that I don't have a clue in the world.
I have a Data Grid View on a form and I want to load the entire contents of a table from an access database into it bar a couple of columns so first of all can someone show me how to fill a datagrid view with a table. I guess from there in the execute sting I just name the columns I want to grab.
Thanks,
Max
Re: [2008] Data Grid View with Access
something like this
Code:
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=c:\MYApp\MyDB.mdb")
Dim adapter As New OleDbDataAdapter
Dim selCommand As New OleDbCommand
With selCommand
.CommandText = "Select Col1,Col2 FROM MYTable"
.CommandType = CommandType.Text
.Connection = conn
End With
'assign select command to data adapter
adapter.SelectCommand = selCommand
Dim dt As New DataTable("MyTable")
adapter.Fill(dt)
dvMyGrid.DataSource = dt
Re: [2008] Data Grid View with Access
Hi, thanks for the reply but I am getting a little stuck. As I am so new to this I don't have a clue really of what does what and I am trying to merge your code with what I am being taught and getting an error. This is what I have so far:
Code:
Dim dbRec As New ADODB.Recordset
Dim dbObj As New ADODB.Connection
Form Load:
Code:
ConnectDB(strDatabasePath & "Data.mdb")
Dim CMD As New ADODB.Command
With CMD
.CommandText = "Select col1,col2,col3,col4 FROM table"
.CommandType = CommandType.Text
End With
'assign select command to data adapter
dbObj.SelectCommand = CMD
Dim dt As New DataTable("table")
dbObj.Fill(dt)
datagridview.DataSource = dt
ConnectDB Routine:
Code:
Private Sub ConnectDB(ByVal Database As String)
Dim intErr As Integer
Database = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" & Database & ";DefaultDir=" & Database
dbObj.Open(Database)
Me.Show()
Call frmSuppliers_Load(Nothing, Nothing)
End If
End Sub
I have edited certain bits to replace column names and table name.
The error is happening on the line:
dbObj.SelectCommand = CMD
The Error:
No named arguments. (Exception from HRESULT: 0x80020007 (DISP_E_NONAMEDARGS))
Is there something that is coded wrong because I really don't have a clue what I am doing here lol.
Thanks,
Max
Re: [2008] Data Grid View with Access
code that you are using is vb6 code while in .NET there is special namespaces and classes to access the database related object.
Look at this ADO.NET Tutorial