Use of the DBGrid control in Visual C ++ Environment
The DBGrid data table control is favored by everyone for its short and refined content, but how it is used in Visual C ++
But few articles mentioned it. This article briefly introduces the specific steps for using DBGrid in VC.
I. Preparations
To realize automatic binding of the DBGrid control, we need to create a database and the associated data source. In this example, we use access to create the database mydata. MDB: Create a new users table in the database, which contains three fields: username, address, and email, and enter multiple records.
Create a system DSN named test in the ODBC data source on the control panel and point it to mydata. MDB
2. Create a project
Create a dialog box-based MFC Appwizard (exe) project named gridtest and
Confirm that ActiveX control support is provided in step 1.
3. Insert controls
You need to insert the Microsoft remotedata control and DBGrid control controls as follows:
1) choose menu> Project> Add to project> components and controls Gallery.
2) In the part selection dialog box, enter registered ActiveX controls.
3) select Microsoft remotedata control and click Insert to confirm and configure the class (you can click
By default), we do not make any changes, click the OK button, the insert is complete.
Insert the DBGrid control in the same step.
4) return to the VC workspace. In resourceview, select the gridtest dialog box template (that is, the dialog box template with the ID number idd_gridtest_dialog). At this time, two buttons are added to the controls toolbar, the two widgets that have just been inserted are displayed.
5) Now we can draw the two controls in the template ,:
4. Modify Control Properties
Right-click the remotedata Control and choose Properties From the shortcut menu. The Properties dialog box is displayed,
On the control tab, select test as the data source and enter an SQL statement, for example, select * from users.
As shown in:
In the same way, open the Properties dialog box of the DBGrid control.
Click the All tab and select the ID number of the remotedata control in the datasource item: idc_remotedatactl1,
As shown in:
The entire program has been completed. We can compile and run it to see how it works! It is worth noting that access should be disabled before running, otherwise the database will not be accessible.
This article introduces the basic usage of the DBGrid control in detail. You can refer to other table controls for some inspiration.
Environment: Visual C ++ 6.0, Windows98