Insert other visual components to DBGrid in Delphi _ Delphi tutorial

Source: Internet
Author: User
Delphi provides powerful DBGrid components to facilitate database application design. However, if we only use the DBGrid component, each grid is a simple text editing box, which is inconvenient for users to input data. Delphi also provides some other data components to facilitate user input, such as dbcombobox and dbcheckbox, but these components do not have the powerful functions of DBGrid. Can Delphi make the focus grid in DBGrid as Visual FoxPro can be another visual data component to facilitate users? In fact, we can achieve this by inserting other visual components in DBGrid.

Delphi's internal mechanism for DBGrid processing is to move a component, dbedit, to the floating point of the grid. The mesh of the data you enter is actually a floating dbedit component, and the rest of the areas where the focus is not obtained are just images. Therefore, inserting other visual components in DBGrid is to move a visible component on the grid. Therefore, all components, including from a simple dbcheckbox to a complex dialog box, can be inserted in DBGrid. The following describes how to insert the dbcombobox component in DBGrid. Other components can be inserted in the same way.

1. Create a new project in Delphi 4.0.

2. Drag the datasource, table, DBGrid, and dbcombobox components on the Data Access Component board to form1.

3. Set the attributes of each component as follows:

Rcf1 Object Property setting
Example of form1 caption inserting the spinedit component in DBGrid
Performance1 dataset Table1
Table1 databasename dbdemos
Tablename teacher. DBF
Active true
Dbgrid1 datasource performance1
Dbcombobox1 datafield sex
Datasource performance1
Visible false
Strings items. Male | female

Meaning: I used teacher. DBF here, which reflects the gender of the faculty and staff. It can only be "male" or "female ".

4. The drawdatacell event is to draw cells. When the fields corresponding to the obtained focus mesh are the same as those corresponding to the combo, move the combo box to the mesh with the obtained focus, and
Make the combo box visible to display dbcombobox in the specified column of DBGrid. The ondrawdatacell event of dbgrid1 is set as follows:
Procedure tform1.dbgrid1drawdatacell (Sender: tobject; const rect: trect; field: tfield; State: tgriddrawstate );
Begin
If (gdfocused in State) then
Begin
If (field. fieldname = dbcombobox1.datafield) then
Begin
Dbcombobox1.left: = rect. Left + dbgrid1.left;
Dbcombobox1.top: = rect. Top + dbgrid1.top;
Dbcombobox1.width: = rect. Right-rect. Left;
Dbcombobox1.height: = rect. Bottom-rect. Top;
Dbcombobox1.visible: = true;
End;
End;
End;

5. dbcombobox is not displayed when the cell specified by DBGrid is not focused. The oncolexit event for dbgrid1 is set as follows:
Procedure tform1.dbgrid1colexit (Sender: tobject );
Begin
If dbgrid1.selectedfield. fieldname = dbcombobox1.datafield then
Begin
Dbcombobox1.visible: = false;
End;
End;

6. When DBGrid specifies that the column gets the focus, the drawdatacell event only draws cells and displays dbcombobox. However, dbcombobox does not get the focus, and the data input is still performed on cells. In the keypress event of dbgrid1, call the sendmessage window system API function to transmit the data input to dbcombobox, so as to input data on dbcombobox. Therefore, you must set the keypress event as follows:
Procedure tform1.dbgrid1keypress (Sender: tobject; var key: Char );
Begin
If (Key <> CHR (9) then
Begin
If (dbgrid1.selectedfield. fieldname = dbcombobox1.datafield) then
Begin
Dbcombobox1.setfocus;
Sendmessage (dbcombobox1.handle, wm_char, word (key), 0 );
End;
End;
End;

The program is successfully debugged in Windows 98 and Delphi 4.015. I hope this article will help you develop database applications more conveniently and quickly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.