Changes

Jump to: navigation, search

Visual Basic and the DataGridView Control

2,839 bytes added, 19:33, 9 August 2007
no edit summary
The Visual Basic ''DataGridView'' control provides a table in which rows and columns from a database table can be displayed and modified. In this chapter we will explore the steps necessary to build a DataViewGrid into a Visual Basic application and connect it to a database table.
The examples in this chapter work with a Microsoft Access database. The concepts covered, however, apply equally to other databases such as Microsoft SQL Server.
The chapter assumes that you have Microsoft Access installed together with the NorthWind sample Access database. This can usually be found in Program Files\Microsoft Office\Office''nn''\Samples\Northwind.mdb" (where ''nn'' signifies your version of Office). If you are unable to locate this database on your system it can be downloaded from [http://www.microsoft.com/downloads/details.aspx?FamilyID=C6661372-8DBE-422B-8676-C632D66C529C&displaylang=en Microsoft's web site].
 
== Connected to a Database ==
 
Begin by launching Visual Studio and creating a new Windows Application project called "VBdatabase". Once the new project has been created the first task is to connect to the Northwind database. Click on the ''View'' menu and select ''Server Explorer''. The Server Explorer panel will appear on the left hand side of the main Visual Studio area:
 
[[Image:vb_server_explorer.jpg | Visual Studio Server Explorer]]
 
This panel allows you to browse servers on your network and locate databases. Click on the ''Connect to Database'' button (highlighted above). The ''Add Connection'' dialog will appear as follows:
 
[[Image:vb_connect_to_database.jpg | Visual Studio Connect to Database]]
 
Change the ''Data source'' field to ''Microsoft Access Database'' if necessary. Click on the ''Browse'' button and locate the ''Northwind.mdb'' database. Once you have selected the Northwind database click on the ''Test Connection'' button to verify the database is accessible. Assuming the connection is successful, click on the ''OK'' button to apply the connection.
 
The database is now connected to the application and it is time to select a data source.
 
== Selecting the Data Source ==
 
Once the database connection is established, the next step is to select the data source to be used in the application. To achieve this, select the ''Data->Add New Data Source'' menu option. The Data Source Configuration Wizard will appear as follows:
 
[[Image:vb_data_source_selection.jpg | Visual Studio Select Data Source]]
 
Since we are selecting data from a database, make sure that ''Database'' is highlighted in this dialog and click ''Next''. Ensure that the Northwind database is listed in the following screen and press ''Next'' to proceed.
 
A dialog will appear asking if you wish to move the database file to your project area. Click ''Yes''. Finally the wizard will display the connection string (this the name used to refer to teh database in your code. Accept the default suggestion and click ''Next''. Finally, you will asked to select the data objects you wish to access from your application. For the purposes of this example click the ''Tables'' option and press ''Finish''.
== Adding a DataGridView Control ==

Navigation menu