Databind recordset to Datagrid


  • Hi,


    I am trying to assign value in my recordset queried from the DB into the Datagrid's datasource during runtime however, I am unable to do so.

    I tried with the following code was prompted with error.

            Dim cn As New ADODB.Connection
            Dim rs As New ADODB.Recordset

            rs.Open("SELECT Name, Address FROM Employee ORDER BY Name", cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)

            If Not rs.EOF Then
                DataGrid1.DataSource = rs

            End If

    Error message during runtime

    A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

    Please help, thanks!


    Tuesday, December 05, 2006 11:42 AM


All replies

  • If your using VB.NET then use ADO.NET to create a dataset and stop using ADO.  There are significant differences and ADO.NET is more suitable.   You ADO recordset is open and Datagrid binding is based upon a disconnected dataset model.  

    If your using ADO because your using an old version of VB such as VB6 then your in the wrong place to be asking VB questions as these forums are for VB.NET questions.


    Wednesday, December 06, 2006 4:00 AM
  • I was a VB6.0 user and I am now trying to migrate to VB Express. Would appreciate if you could share some samples on how can I bind my data returned from my query to my datagrid during runtime using the ADO .Net
    Wednesday, December 06, 2006 4:14 AM
  • Hi,

    First you need to create a dataset. Project/Add new item/Dataset. I'll assume you call it AppData. The dataset will become the data layer of your application.

    In the dataset designer, drag all your tables from the server explorer. The server explorer is accessed from View/Server Explorer. You can add a new data connection to your database (sql server, access, odbc...). Save and compile.

    Create a new form. Open the Data Source pane (data/show data source) and you will see your dataset as a datasource. I'll assume you'll create a form for table Foo. Next you need to decide if you want a grid view or a detail view. Click on the Foo table in the data source window and you will see a dropdown appear. If you want a detail view instead of a grid change it with this dropdown.

    Now drag the table to the empty form. You will see that a grid (or text boxes and labels) will be created with each of the fields. A BindingSource object, a binding navigator, a table adapter and an instance of your dataset are also created. Now switch to code view. You will notice in the Form_Load event some code to load the data and on the save toolstrip code to end editing and validate the data.

    Let's suppose you also want to save back the changes to the database. Add this line of code:


    That's it. Now you have an application that can insert, delete, update and browse all the data in table Foo.


    Wednesday, December 06, 2006 4:54 AM
  • Look at the building data centric applications section



    Also some web cast videos showing you how the new features work including data binding



    Also a search on VB.NET and data binding will reveal many articles with examples



    Wednesday, December 06, 2006 5:23 AM