To Documents

Groceries1 Example Directions

Connecting a Database Table to a DataGrid Object Without Writing Code

  1. Download and unzip the file Groceries.zip. Put the resulting file Groceries.mdb in the folder c:\Database.

  2. Create a Windows Application called Groceries1.

  3. Add a DataGrid control to Form1. Name it dgGroceries.

  4. Create and configure an OleDbConnection object:

    1. Select the Data tab in the Toolbox. Place an OleDbConnection object on the form. Change its name to connectionObject.

    2. Click on the right arrow of the ConnectionString object and select "<New Connection...>". This brings up the Data Link Properties Dialog.

    3. On the Provider Tab, select "Microsoft Jet 4.0 OLE DB Provider".

    4. Click "Next>>" to move to the Connection Tab. Select the database as c:\Database\Groceries.mdb. Leave the user name as Admin and leave Blank Password checked.

    5. Click OK .

    6. Click "Don't Include Password" button.

  5. Create and configure an OleDBDataAdapter object:

    1. Find OleDbDataAdapter on the Data tab of the Toolbox and place an instance of it on Form1. This will bring up the Data Adapter Configuration Wizard.

    2. Click "Next >" to continue.

    3. Select this connection ACCESS.c:\Database\Groceries.mdb in the combo box and click on "Next >".

    4. Leave "Use SQL Statements" checked and click on "Next >".

    5. Enter this SQL statement in the textbox:
      SELECT * FROM GroceryList
      Then click "Next >".

    6. Click "Finish" to apply the settings to the OleDBDataAdapterObject.

    7. Change the name of OleDBDataAdapter1 to dataAdapterObject.

  6. Create and configure the DataSet object corresponding to the SQL statement
    SELECT * FROM PersonData.

    1. On the main menu, select "Data" and "Generate Dataset..." to bring up the Generate Dataset window.

    2. Leave "New" checked and enter PersonDataset as the name of the dataset. Leave PersonDataset (dataAdapterObject) checked. Also leave Add this dataset to the designer checked. Click OK.

    3. Change the name of PersonDataset1 to datasetObject.

  7. Create Form_Load event handler for Form1. Add this code to it:
    Dim n As Integer
    n = dataAdapterObject.Fill(datasetObject)
    then set the DataSource property of the DataGrid object to the GroceriesList table of datasetObject.

  8. Run the project. The DataGrid object should display the GroceryList table of Groceries.mdb.