Results 1 to 4 of 4

Thread: [RESOLVED] [2005] newbie - Bind DataGrid with data (Access DB)

  1. #1

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Resolved [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.

  2. #2
    Lively Member SharkC382's Avatar
    Join Date
    Jul 2007
    Location
    Kennesaw, Ga
    Posts
    68

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    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
  •  



Click Here to Expand Forum to Full Width