How to Use Visual Basic Grid Control
Visual Basic comes with several grid controls. The most common is the DataGrid control. Typically, this displays data in a database table.This control belongs to a Microsoft file labeled the MSDATGRD.OCX file. Any application you build that uses the DataGrid control must distribute this file along with the application itself in order to work. In addition, the control requires a data source connection such as an ActiveX Data Object (ADO) in order to connect the grid to a database. The MSADODC.OCX file contains this control.
Instructions
-
-
1
Open Visual Basic and create a new project by clicking on “File” and then “New Project.” Select “Standard EXE” from the list of Visual Basic templates. Name the project DataGrid Demo.
-
2
Add the necessary controls for this project to the ToolBox on the left of the screen. Click “Project” on the top menu and choose “Components." Scroll down in the list of components until you find “Microsoft ADO Data Control 6.0” (Adodc) and click on the check box to add this component. Do the same thing for the “Microsoft DataGrid Control 6.0.” Click “OK.” Both controls should now appear in the ToolBox.
-
-
3
Double-click on the DataGrid control to add it to the form. This control appears in the ToolBox as an icon shaped like two tiny columns with a small, clock-like circle. Drag this control on the form towards the upper left of the form. Double-click on the ADO Data control, which is an icon with two tiny boxes containing left and right arrows and a circle, which looks like a clock with hands at a quarter of one. Drag this to the bottom of the form.
-
4
Establish a data connection using the ADO Data Control. The full procedure involves multiple steps beyond the scope of this article.You can access a Wizard to complete this step by right-clicking on the “Adodc” control added above and selecting “ADODC properties.” Follow each step of the Wizard carefully. You will need to know such things as the name and type of the Data Source (Microsoft Access or another type of OLE DB (Object Linking and Embedding Database)) provider, where to find it on the computer and what table from which to retrieve data.
-
5
Click on the DataGrid control to view the properties in the right-hand panel on the screen and modify the appearance as desired using the different available properties. Right-click on the DataGrid control to open a custom properties box. Make changes such as the column names, colors, fonts and format (General, Number, Currency and so forth) to match the data in your database. Other options control whether a user can add, update or delete data.
-
6
Press “F5” to test the preceding steps. Data from your database should fill the DataGrid if done properly.
-
1
Tips & Warnings
The last step in the Adodc connection Wizard displays a link to test the connection to the database. Do not proceed unless the connection test is positive. If it fails, recheck the items in the connection Wizard, particularly the connection string or data source name.
Working with the DataGrid control as it runs is complex. Because of the possibility of inadvertently altering your database source, always work with a backup.
References
- "Programming Visual Basic 6.0"; Francesco Balena; 1999
- Temple University: Data Grid and ADO Data Control
Resources
- Photo Credit BananaStock/BananaStock/Getty Images