When people use Delphi to develop database applications, they often use tabular controls to make reports. The flexibility to use the Ondrawcell event of a tabular control enables the display of some special effects to better meet the user's needs. This article describes three tips for using the Ondrawcell event flexibly.
Dynamically update the color of a table row
Sometimes you need to dynamically update the color of a table row in a report. For example, in the list of suppliers, preferred suppliers are shown in green, and other suppliers are shown in red. This can be implemented in the Ondrawcell event with the following code:
If Table1.fieldbyname (' Custno '). Asinteger >
DBGrid1.Canvas.Font.Color: = clred;
Dbgrid1.defaultdrawcolumncell (Rect, Datacol,
Column, State)
The code above can be expanded to suit the needs of the application. For example, if you want to display in bold, just change the corresponding line to:
DBGrid1.Canvas.Font.Style: = [Fsbold];
Inserting other visual controls in a table
In the database programming, the FOREIGN KEY constraint is an effective method to ensure the data in the database is stored according to user requirements. For example, in an order processing environment, there is a foreign key constraint between the order table and the Customer table, that is, each record in the order table should have a corresponding item in the Customer table. Thus, when filling out the order form, if the customer list is provided in the input focus for the user to select, it will effectively guarantee the correctness of the data.
Both the DBGrid control and the Stringgrid control are derived from Tcustomgrid, where the DBGrid control can enter data. Delphi's internal mechanism for DBGrid processing is to float a dbedit control on the grid. The grid that gets the focus, the input data, is actually a floating dbedit control, and when the focus shifts, the text in the edit box is displayed to the table, and the rest of the focus is not the same as the Stinggrid, which makes it possible to enter data on the table. So, the way to insert other visual controls in a table is to float the visual control on the grid. So, in principle, any control can be inserted in a table. This article takes the example of inserting a drop-down box into a table, describing the steps to insert other controls on the grid.
The result of the operation is shown in Figure 1:
Figure 1
1. Setting properties
Read the list of customers into the items property of the drop-down box.
2. Setting the Ondrawcell Event
The Ondrawcell event is triggered when the cell of the table is drawn. When the cell that gets the focus corresponds to the field that corresponds to the drop-down box, move the drop-down box to the grid that has the focus, and make the drop-down box visible to display the drop-down box on the table-specified column. The Ondrawcell event for setting up the table is as follows:
if (gdfocused) then
Begin
Corresponding fields are executed in the same
if (grid.cells[acol,0]= ' company ') then
Begin
Move the drop-down box to the specified position
Combobox1.left: = Rect.left + grid.left;
Combobox1.top: = Rect.top + grid.top;
Combobox1.width: =rect.right-rect.left;
Combobox1.height:=rect.bottom-rect.top;
Make the drop-down box visible and get focus
Combobox1.visible: = True;
Combobox1.setfocus;
End
End
3. Setting the OnClick Event
The drop-down box is not visible if the cell that receives the focus does not correspond to the field that corresponds to the drop-down box. To do this, set the OnClick event for the table as follows:
If (grid.cells[grid.col,0]<> ' company ') then
Begin
Combobox1.visible: = false;
End
4. Setting the onchange Event
Only the work of drawing cells and displaying drop-down boxes is completed in the Ondrawcell event, and data cannot be entered into a table. To do this, set the onchange event for the drop-down box as follows:
Grid.cells[grid.col,grid.row]:=combobox1.
Items[combobox1.itemindex];
This completes the work of inserting a drop-down box in the table.
Display graphic fields in a table
In program development, it is sometimes necessary to display some unconventional characters in the report. For example, the Inspection item field in the part inspection report contains non-regular characters such as the workpiece roughness, machining tolerances, and so on. The general idea for solving this problem is to store these unconventional characters as graphic fields, such as the Image field in a SQL Server database. However, either the Stringgrid control or the DBGrid control can only display the string directly, resulting in problems with how to display the graphics field on the table control.
At first glance, the problem can be solved by inserting the Dbimage control into the table using the above method. However, it is necessary to insert a control with the same number of records, the more troublesome is because the Ondrawcell event triggered very frequently, so that the inserted dbimage control will not stop trying to connect with the database, not only increase the network traffic, and the graphics field obviously flicker sense, affecting the normal operation of the program. A better solution is to dynamically create the Dbimage control when you connect to the database, use the control to read the image field of the database, and save it as a temporary file to the client hard disk. Reads the temporary file in the Ondrawcell event of the table and completes the drawing work, which is deleted when the client exits. The following are the steps to display a graphic field in a table.
The result of the operation is shown in Figure 2:
Figure 2
1. Save Image Field
Reads all the image fields when the query is executed and saves them to the client hard disk.
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.