Home
| Work & Business
| Computer Software
| Visual Basic
| Visual Basic Tutorial Datagrid
Visual Basic Tutorial Datagrid
by Lysis
-
Overview
The datagrid is a component that lists data in an application. For instance, if the developer is creating a list of customers in a sales application, the customer's name, phone number and address can be listed in a matrix using the datagrid component. The datagrid is populated using database information, so a call to the server is necessary for population. Once the database is called, populating the datagrid is accomplished using a few lines of code.
-
The Control
The control is available through the Visual Basic application toolbar. The control is added using the "Add Component" dialog box found in the Project menu. Once the control is added to the toolbar, drag and drop it onto a Visual Basic form. The datagrid control looks like a table or Excel spreadsheet. It's a matrix of rows and columns. The rows are records, and a datagrid can have several rows. Columns are the specific pieces of information from the database. For instance, a column for a customer can be a first or last name. The intersection of a column and row is a field. A field makes up one piece of information in a record. These points are important to understand, since the programming for the data control requires placement of these datagrid components.
-
Configuring the Datagrid
Right-clicking on the control and selecting properties gives the developer the option to specify where the data is located and how it should be displayed. When first opening the configuration window, the wizard asks for the connection string. The connection string defines the location of the server and what database is used to retrieve the information. Clicking the "Build" button prompts the programmer for the name of the server and the name of the database.
Once the server and connection string have been defined, the connection tab is used to define any type of security settings. Most database servers require a username and password before querying the database. Enter the name of a user with the password to use for querying. This username and password is used every time the application opens the window containing the datagrid.
Finally, the Record Source tab defines the type of connection. If the server uses a stored procedure to query information, enter the stored procedure name in this tab. If a query was created, enter the query code in this tab as well. Once these settings are defined, click the "OK" button and the datagrid will populate with the column headers, indicating that the settings are correct.
Once the datagrid has been configured, start the application in debug mode to verify that the control is working properly. If any part of the settings are incorrect, Visual Basic will return an error. If everything is configured properly, the data is retrieved from the database and populated in the datagrid control.