» Home
» Tutorials
» Search  
» Contact us 

Home » Tutorials » Introduction to Visual Basic .NET » Chapter 10

Chapter 10: Working with Databases
10.5: Data Binding
10.5.1: Binding Data to a DataGridView Control

Binding Data to a DataGridView Control

In this Exercise, you will be using the data access wizards in Visual Studio 2005 to create the data components necessary to bind data to a DataGridView control. You will be using the Northwind.mdb sample database again as your data source.

 

Exercise - Binding Data to a DataGridView Control

1. Create a new Windows Application project called Northwind Customers DataGridView.

2. Click the Data tab in the ToolBox and then drag a DataGridView control from the ToolBox and drop it on your form. The DataGridView control will display the Tasks dialog box as shown:

3. Click the drop-down arrow in the Choose Data Source combo box and then click the Add Project Data Source link at the bottom of the list that is displayed. This will cause the Data Source Configuration Wizard to be displayed.

4. The Choose a Data Source Type screen allows you to choose the data source for your data. As you can see from this screen, shown in below Figure, you have several options for a data source.

You can click the Database icon for connecting to various databases such as SQL Server, Oracle, and Access, the Web Service icon for connecting to a Web Service, and the Object icon for connecting to your business logic components. Click the Database icon and click the Next button.

 

5. In the Choose Your Data Connection screen, click the New Connection button.

6. In the Choose Data Source dialog box, select Microsoft Access Database File in the Data Source list and then click the Continue button.

7. In the Add Connection dialog box, click the Browse button and navigate to the samples folder for Microsoft office. By default, this will be in the folder C:\Program Files\MicrosoftOffice\Office11\Samples\ for a default installation of Microsoft Office 2003.

Select the Northwind.mdb database in the Select Microsoft Access Database File dialog box and click the Open button to have the path and file name added to the text field on the Add Connection dialog box. Click the OK button when you are done to close the Add Connection dialog box and then click the Next button on the Choose Your Data Connection screen.

You will be prompted with a dialog box that informs you that the data file is not part of your project and asks if you want to add it. Click the Yes button in this dialog box.

 

8. Click the Next button on the Save the Connection String to the Application Configuration File screen.

9. The Choose Your Database Objects screen allows you to select the data that your application needs. Here you have the option to select data directly from the tables in your database, data generated from the execution of various views and stored procedures or data generated from the execution of functions.

You’ll be using the query that you created in the last exercise so expand the Views node in the Database objects list and then check the check box for CustomerQuery as shown in below Figure. If you expand CustomerQuery, you’ll see the columns that are returned from this query. Click the Finish button when you are done.

At this point, the wizard will generate a DataSet object named NorthwindDataSet, a BindingSource object named CustomerQueryBindingSource, and a TableAdapter object named CustomerQueryTableAdapter.

 

10. Since you will not be adding, editing, or deleting records from this table, uncheck the check box next to these options in the Tasks dialog. You will, however, want to implement sorting in your DataGridView component, so check the check box next to Enable Column Reordering. When you are done, click on the title bar of the form to hide the Actions dialog.

11. Click the DataGridView control and, in the Properties window, set the Dock property to Fill.

12. At this point you can run your project to see the results. Click the Start button on the toolbar, and your form will be displayed with the DataGridView control populated with data.

You can click on the column headers to have the data in the DataGridView sorted in ascending order. Clicking the same column header again will sort the data in descending order. Each sort order will be indicated with an arrow pointing up for ascending and down for descending.

At this point you have not written a single line of code to achieve these results, which just goes to prove how powerful the data wizards in Visual Basic .NET are.

 

click next Page - How It Works.

Home | Link to Us | Partner Links | About us | Contact us

Copyright © 2009-2012 F1tutorials.com | All Rights Reserved