|
-
Jul 8th, 2007, 06:21 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] newbie - Bind DataGrid with data (Access DB)
Hi.
I want to make a function to bind DataGrid with Access Data tables.
This is my code, which is not working.
What am I doing wrong?
Code:
Public sConnString As String = _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=D:\VBProjects\GestaoProjecto\BaseDados\GesProj.mdb;" & _
"Uid=Admin;" & _
"Pwd="
Function OpenDataSet(ByVal strSQL As String) As DataSet
Dim ds As DataSet
Dim DataAdapt As Odbc.OdbcDataAdapter
DataAdapt = New Odbc.OdbcDataAdapter(strSQL, sConnString)
ds = New DataSet()
Return ds
End Function
On my form, I've got this code to bind my datagrid.
Code:
Private Sub FormArtigos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CarregaArtigos()
End Sub
Private Sub CarregaArtigos()
Me.DataGridView1.DataSource = OpenDataSet("Select * from Artigos")
End Sub
Thank you.
-
Jul 8th, 2007, 11:11 PM
#2
Lively Member
Re: [2005] newbie - Bind DataGrid with data (Access DB)
Well, 2 things need to happen. You need to open the database to get to the data, and you need to use the dataadapter to "fill" the dataset. Also, since a dataset can have multiple tables, you'll want to name your table, and use that name as the .DataMember value on your DataGridView.
Code:
Function OpenDataSet(ByVal strSQL As String) As DataSet
Dim ds As DataSet
Dim DataAdapt As Odbc.OdbcDataAdapter
Dim conn As New Odbc.OdbcConnection(sConnString)
DataAdapt = New Odbc.OdbcDataAdapter(strSQL, sConnString)
ds = New DataSet()
conn.open
DataAdapt.Fill(ds, "MyTable")
Return ds
End Function
And then after you set .DataSource, you need to set the .DataMember.
Me.DataGridView1.DataMember = "MyTable"
Regards
-
Jul 9th, 2007, 02:10 AM
#3
Re: [2005] newbie - Bind DataGrid with data (Access DB)
A DataSet is pointless if all it contains is one DataTable. Just have your method create and return a DataTable instead. You can then assign that to the DataSource property and not worry about the DataMember.
-
Jul 11th, 2007, 09:02 AM
#4
Thread Starter
Hyperactive Member
Re: [2005] newbie - Bind DataGrid with data (Access DB)
Thank you. That really helped me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|