When you scroll up and down through the data records of the dataset, you are not scrolling through the rows of the DBGrid the rows are stationary, and the data moves from one row to the next to give the appearance of movement. Unlike the generic grid used earlier, a DBGrid is a virtual view on the dataset-there is no relation between the number of rows shown on the screen and the number of rows of data in the dataset.
The lines of code for that operation may be few, but they cost me hours of work! Notice the output of the graphics and memo fields.Īlthough creating the output was a simple matter of adapting the code used in the record viewer component, setting the height of the grid cells ended up being a difficult problem to solve. Therefore, the KeyPress event must also be set as follows: procedure TForm1.DBGrid1KeyPress (Sender: TObject var Key: Char) begin if (key chr (9)) then begin if ( = DBComboBox1.DataField) then begin DBComboBox1.SetFocus SendMessage (DBComboBox1.Figure 17.4: An example of the MdDbGrid component at design time.
Call the SendMessage Windows API function in the KeyPress event of DBGrid1 to transfer the data input to the DBComboBox, so as to achieve data input on the DBComboBox. The DrawDataCell event only draws the cell and displays the DBComboBox when the DBGrid specified column gets focus, but the DBComboBox does not To get the focus, the data input is still on the cell. Set the OnColExit event of DBGrid1 as follows: procedure TForm1.DBGrid1ColExit (Sender: TObject) begin If = DBComboBox1.DataField then begin DBComboBox1.Visible: = false end end - 6. DBComboBox is not displayed when the specified cell of the DBGrid does not receive focus. Top DBComboBox1.Visible: = True end end end - 5. 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: = DBComboBox1.Height: = Rect.Bottom-Rect. When the field corresponding to the focus grid is the same as the field corresponding to the combo box, move the combo box to the grid that has focus and make the combo box visible So as to achieve the function of displaying DBComboBox on the designated column of DBGrid. The DrawDataCell event is to draw a cell. 'Male' | 'Female' - Note: I used Teacher.dbf here, it reflects the gender of the teaching staff, it can only be "male "Or" female ". Set the properties of each component as follows: rcf1 object property setting plant Form1 Caption 'Example of inserting SpinEdit component in DBGrid' DataSource1 DataSet Table1 Table1 DatabaseName DBDEMOS TableName 'teacher.DBF' Active True DBGrid1 DataSource DataSource1 DBComboBox1 DataField SEX DataSource DataSource1 Visible False Strings Items. Drag the DataSource, Table, DBGrid, DBComboBox on the Data Access component board respectively to Form1.
The following is a step how to insert the DBComboBox component in the DBGrid, using the same method can insert other components. Therefore, any component, from simple DbCheckBox to complex dialog box, can be inserted in DBGrid.
Therefore, inserting other visual components in DBGrid is to float a visual component on the grid. The grid you input data is actually a floating DBEdit component, other places that don't get focus are just images. Delphi's internal mechanism for DBGrid processing is to float a component on the grid-DBEdit component. Can Delphi, like Visual Foxpro, let DBGrid get the focus grid can be other visual data components to facilitate users? In fact, we can achieve this by inserting other visual components in the DBGrid. 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.
But if we only use the DBGrid component, each grid (Grid) is just a simple text edit box, which is not convenient for users to enter data. Delphi provides a powerful DBGrid component to facilitate the design of database applications.