Populating DataGridView with Access 2007 Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WestAussie
    New Member
    • Oct 2007
    • 10

    #1

    Populating DataGridView with Access 2007 Query

    Hello

    I'm trying to populate a DataGridView with a stored procedure using VB.NET.
    The query I need to use is within a Access 2007 datadase. When I run it in Access it is showing data but the DataGridView displays nothing.

    The code I'm using to populate the DataGridView is:

    ------------------------------------------------------------------------------
    'Create connection object.
    CConnection = New CDatabaseConnec tion
    With CConnection
    .NewMSAccessCon nection(strFile Path)
    .OpenDBConnecti on()
    End With

    'Initialize a new instance of the OleDbCommand class.
    objCommand = New OleDbCommand

    'Set command object properties.
    objCommand.Comm andText = "usp_AccessQuer y"
    objCommand.Comm andType = CommandType.Sto redProcedure
    objCommand.Conn ection = CConnection.obj Connection

    'Initialize a new instance of the Adapter class.
    objDataAdapter = New OleDbDataAdapte r

    'Initialize a new instance of the DataSet class.
    objDataSet = New DataSet

    'Set the SelectCommand for the DataAdapter.
    objDataAdapter. SelectCommand = objCommand

    'Populate the DataSet.
    objDataAdapter. Fill(objDataSet )

    Me.DataGridView 1.DataSource = objDataSet
    ------------------------------------------------------------------------------

    I tried to populate a DataGrid using the same code sequence and found I had to select the expand icon within the grid to show a list of all the query columns with headings, but no data was shown.

    Any ideas as to why this is happening?
    Since I'm going to be doing more of this type of programming, does anyone know of some good sites with tutorials for working with Access databases in VB.NET?

    Thanks.
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Try adding:


    Me.DataGrid1.Da taBind()

    after the line where you set the data source...

    Comment

    • WestAussie
      New Member
      • Oct 2007
      • 10

      #3
      Originally posted by balabaster
      Try adding:


      Me.DataGrid1.Da taBind()

      after the line where you set the data source...
      Thanks for your reply balabaster.

      I tried your suggestion but the following error occurred.

      'DataBind' is not a member of 'System.Windows .Forms.DataGrid View'.

      Since I posted my original question I have experimented using a DataTable and linking that directly to the table in the database. When I used a DataGrid the correct information was displayed. When I went to use the DataTable with the DataGridView no table content displayed, not even column headings.

      Rather frustrating since all the code examples appear to work in the books I'm using.

      Also, I've added a BindingSource and BindingNavigato r to the form which I'm hoping to be able to set parameters at run time. So far I haven't been able to display the data in order to test these objects.

      Any other ideas I could try?

      Comment

      • ISATownsME
        New Member
        • Oct 2008
        • 3

        #4
        Hello, I was wondering if you would know how to reverse the process meaning populating Access 2007 from DataGridView

        Comment

        Working...