Add colors for the DBGrid control in Delphi

Source: Internet
Author: User

 

 

 

Add colors for the DBGrid control in Delphi

Author: knife light dance Source: Skynet responsibility Editor: Ark

 

 

Adding different colors to the TDBGrid component will enhance the display appearance of the component and differentiate the rows or columns of different content in the database.

The TDBGrid component is a great component used to display data content. The focus of this article is to explain how to change the color of a Specific Row, column, or cell in TDBGrid.

Preparation phase:

This document assumes that you already know how to connect a TDBGrid to a database. The easiest way is to use "database Form Wizard" to connect TDBGrid to dbdemo (the employee in the database provided by Delphi. join the database and select all fields except the limit T.

Color the TDBGrid

Color Columns

The first type is the easiest way to color a column, which is most directly visible to your users.

We use the tcolumns attribute of TDBGrid. The procedure is as follows:

Select the TDBGrid component on the form, and double-click the columns attribute of TDBGrid in object inspector to open the columns editing window. (For more information, refer to the topic "columns Editor: creating persistent columns" in Delphi help .)

What you need to do is to specify the background color of the line or line that you need to change the color. If you want to change the font color, modify the color attribute in the font attribute set.

 

Figure 1

 

Just a few clicks, and the modified TDBGrid is no longer a dull white background.

Color rows

1st if you want to specify a color for a grid or grid selected in TDBGrid, and you do not want to use the dgrowselect option, because you want TDBGrid to directly edit data in the TDBGrid cell, you should use the ondrawcolumncell event of TDBGrid.

The following techniques can be used to dynamically change the color of cell text in TDBGrid.

The Code is as follows:

Procedure tform1.dbgrid1drawcolumncell (Sender: tobject; const rect: trect; datacol: integer; column: tcolumn; State: tgriddrawstate );

 

Begin

If table1.fieldbyname ('salary '). ascurrency> 36000 then

// Specify the conditional expression for changing the color row

Dbgrid1.canvas. Font. Color: = clmaroon;

// Specify the color as clmaroon

Dbgrid1.defadrawcolumncell (rect, datacol, column, State );

End;

The above code execution function is to mark the font color of the employee (employee) whose salary is more than 30 thousand RMB with maroon.

 

Figure 2

 

The following code shows how to dynamically change the color of the row in TDBGrid:

Procedure tform1.dbgrid1drawcolumncell (Sender: tobject; const rect: trect; datacol: integer; column: tcolumn; State: tgriddrawstate );

 

Begin

If table1.fieldbyname ('salary '). ascurrency> 36000 then

Dbgrid1.canvas. Brush. Color: = clwhite;

Dbgrid1.defadrawcolumncell (rect, datacol, column, State );

End;

The above code execution function is to mark the background of employee (employee) records with a salary of more than 30 thousand RMB with white.

 

Figure 3

 

The following code modifies the background color of some cells in a specified column:

Procedure tform1.dbgrid1drawcolumncell (Sender: tobject; const rect: trect; datacol: integer; column: tcolumn;

 

State: tgriddrawstate );

Begin

If table1.fieldbyname ('salary '). ascurrency> 40000 then

Begin

Dbgrid1.canvas. Font. Color: = clwhite;

Dbgrid1.canvas. Brush. Color: = clblack;

End;

If datacol = 4 then

Dbgrid1.defadrawcolumncell (rect, datacol, column, State );

End;

The above code is executed to mark the employee (employee) record background with a salary of more than 40 thousand with black and the text with white.

This is convenient, because you are using Delphi, which is like advertisement for borland. Oh, my program is compiled on Delphi7 + WINXP and Delphi + Windows2000, you may try it.

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.