Delphi provides the powerful DBGrid component to facilitate database application design. But if we only use the DBGrid component, each focus (Grid) is just a simple text editing box, which is inconvenient for users to enter data. Delphi also provides some other data components to facilitate user input, such as DBComboBox, DBCheckBox and other components, but these components are not as powerful as DBGrid. Can Delphi, like Visual FoxPRo, allow the focus grid in DBGrid to be other visual data components for the convenience of users? In fact, we can achieve this by inserting other visual components into the DBGrid.
Delphi's internal mechanism for processing DBGrid is to float a component on the grid - the DBEdit component. The grid where you enter data is actually a floating DBEdit component, and the other areas that don't get focus are just images. So, inserting other visual components into the DBGrid is like floating a visual component on the grid. Therefore any component, from a simple DbCheckBox to a complex dialog box, can be inserted in DBGrid. The following is a step on how to insert the DBComboBox component into DBGrid. You can insert other components in the same way.
1. Create a new project in Delphi 4.0.
2. Drag the four components of DataSource and Table on the Data access component board and DBGrid and DBComboBox on the Data Controls component board to Form1 respectively.
3. Set the properties of each component as follows:
rcf1 object attribute setting plant
Form1 Caption 'Example of inserting SpinEdit component into DBGrid'
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'
Note: I used Teacher.dbf here, which reflects the gender of the faculty and staff, which can only be "male" or "female".
4. The DrawDataCell event is to draw cells. When the field corresponding to the focused grid is consistent with the field corresponding to the combo box, move the combo box to the focused grid, and
Make the combo box visible to achieve the function of displaying the DBComboBox on the specified column of the DBGrid. Set the OnDrawDataCell event of DBGrid1 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. The DBComboBox is not displayed when the specified cell in DBGrid does not receive 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. When the specified column of DBGrid gets the focus, the DrawDataCell event only draws the cell and displays the DBComboBox, but the DBComboBox does not get the focus, and data input is still performed on the cell. Call the SendMessage Windows API function in the KeyPress event of DBGrid1 to transfer data input to the DBComboBox, thereby achieving data input on the DBComboBox. So also 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 passed debugging under Chinese Windows 98 and Delphi 4.015. I hope this article will enable you to develop database applications more conveniently and quickly.