Insert other visual components in Delphi DBGRID

zhaozj2021-02-11  198

Insert other visual components in Delphi DBGRID

Delphi provides powerful DBGRID components to facilitate database application design. But if we use the DBGRID component, each get the focus (Grid) is just a simple text editing box, which is not convenient for users to enter data. Delphi also provides some other data components to facilitate user input, such as DBCombOBox, Dbcheckbox, etc., but these components have no DBGRID functionality. Can Delphi can make DBGRIDs like Visual FoxPro can be other visual data components to facilitate users? In fact, we can achieve this by inserting other visual components in DBGRID. Delphi's internal mechanism for DBGRID is a component-dbedit component that floats on the grid. The grid you enter the data is actually a floating dbedit component, and the other unfolded places is just an image. So, inserting other visual components in DBGRID is a visual component that floats on the grid. Thus any component, including from a simple dbcheckbox to complex dialog box, can be inserted in DBGRID. Below is a step of inserting a DBCOMBOBOX component in DBGRID, using the same approach to insert other components. 1, create a new project in Delphi 4.0. 2, Data Access component board dragged on DataSource, Table, Data Controls component board on DBGRID, DBCOMBOBOX, four components to FORM1. 3, provided each component properties are as follows: rcf1 Object attribute setting explants Form1 Caption 'inserted in a DBGrid exemplary assembly SpinEdit' DataSource1 DataSet Table1 Table1 DatabaseName DBDEMOS TableName 'teacher.DBF' Active True DBGrid1 DataSource DataSource1 DBComboBox1 DataField SEX DataSource DataSource1 Visible False Strings Items. 'Male' | 'Female' mean: I have used Teacher.dbf here, which reflects the gender of the teachers, can only be "male" or "female". 4, DrawDATACELL event is to draw cells, when the field corresponding to the focus grid is consistent with the fields corresponding to the combo box, the moving combination box is on the grid that gets the focus, and the combo box is visible, thereby reaching in DBGRID The function of displaying DBCOMBOBOX is displayed on the column.

The following events OnDrawDataCell provided DBGrid1: 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, DBGRID Specifies that the cell does not display DBCOMBOBOX when the unit is not focus, set the oncolexit event of DBGRID1 as follows: Procedure TFORM1.DBGRID1COLEXIT (Sender: TOBJECT); begin if DBGRID1 .SelectedField.FieldName = DBComboBox1.DataField then begin DBComboBox1.Visible: = false; end; end; 6, is obtained when the designated column DBGrid DrawDataCell event focus only draw the cell, and display DBComboBox, but not DBComboBox focus, data Input or on a cell. Tune the SendMessage this Windows API function in DBGRID1, which is transferred to DBCOMBOBOX, which reaches data input on DBCOMBOBOX. Thus also provided 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; program in Chinese Windows 98, Delphi 4.015 is debugged. I hope this article can make you more convenient and quick to develop database applications. Author: No Source: No author email: No author's website: No

转载请注明原文地址:https://www.9cbs.com/read-4225.html

New Post(0)