Add color to Delphi data table

Source: Internet
Author: User
Tags textout

Delphi Data Control Component DBGrid is the most important and most commonly used component to reflect the data table. In an application, if you display DBGrid in color, you will increase its visibility, especially when displaying important or alert data, changing the foreground and background color of the row or column in which the data resides.

The DBGrid property defaultdrawing is used to control the rendering of the cell (grid). If the default setting of Defaultdrawing is true, it means that Delphi uses the DBGrid default drawing method to make the grid and the data contained therein. The data is drawn by the DisplayFormat or editformat characteristics of the Tfield component connected to a particular column, and the grid or its contents is not drawn if the Defaultdrawing property of the DBGrid is set to False,delphi. You must provide your own drawing routines (custom painting) in the Tdbgrid Ondrawdatacell event.

An important attribute of DBGrid is used here: the canvas is canvas, and many artifacts have this attribute. Canvas represents the surface of the currently displayed DBGrid, you can display the Canvas property value on the screen if you assign a separate display content and style to the Canvas,dbgrid object of the DBGrid object. When applied, the brush attribute and FillRect method and TextOut method of canvas are involved. The Brush property stipulates the image, color, style, and access to the Windows GDI object handle that the Dbgrid.canvas displays, and the FillRect method fills the rectangular area with the current brush property, by way of textout the text content of the canvas output.

Here is an example of how to display color DBGrid in detail. In the example, there is a DBGrid component, followed by a spinedit widget for color filtering, and a Colorgrid widget for free selection of the foreground and background color of the data unit.

1. Create project named Colordbgrid, place the required widget in its form Form1, and set the property to the corresponding value, as listed below:

Table1 DatabaseName: DBDEMOS
TableName: EMPLOYEE.DB
Active: True;
   DataSource1 DataSet: Table1
   DBGrid1 DataSource1: DataSource1
DefaultDrawing: False
   SpinEdit1 Increment:200
Value: 20000
   ColorGrid1 GridOrdering: go16*1

2. Write a responder for the DBGrid1 widget Ondrawdatacell event:

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;Field: TField; State: TGridDrawState);
   begin
   if Table1.Fieldbyname(′Salary′).value=SpinEdit1.value then
   DBGrid1.Canvas.Brush.Color:=ColorGrid1.ForeGroundColor
   DBGrid1.Canvas.Brush.Color:=ColorGrid1.BackGroundColor;
   DBGrid1.Canvas.FillRect(Rect);
   DBGrid1.Canvas.TextOut(Rect.left+2,Rect.top+2,Field.AsString);
   end;

The role of this process is when SpinEdit1 given conditions are met, such as ′salary′ When a variable is below or equal to Spinedit1.value, the DBGRID1 record is displayed in the foreground color of ColorGrid1, otherwise the COLORGRID1 background color is displayed. It then calls the DBGrid Canvas fill procedure fillrect and the text output process to redraw the DBGrid screen.

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.