Delphi Change DBGrid Color Tips

Source: Internet
Author: User



1. Change the color of a cell according to conditions





[Delphi]

Procedure Tmainfrm.  First_dgdrawcolumncell (Sender:tobject;
Const Rect:trect; Datacol:integer;  Column:tcolumn;
State:tgriddrawstate);
Begin
if DM. Firstliststatus. asstring=' production ' then begin
FIRST_DG. Canvas. Font. Color:=clgreen
End
else begin
FIRST_DG. Canvas. Font.  color:=clred;
end;
If Datacol = 4 Then
FIRST_DG.  Defaultdrawcolumncell (rect,datacol,column,state);
End




2, a variety of ways to change





[Delphi]

<span style= "color:#000000;" >procedure tmainform.  Dbgrid1drawcolumncell (Sender:tobject;
Const Rect:trect; Datacol:integer; Column:tcolumn;  State:tgriddrawstate);
var i:integer;
Begin
if gdselected in and then Exit;
Define the font and background color of the header:
For i: =0 to (Sender as TDBGrid). Columns. count-1 do
begin
(Sender as TDBGrid). Columns[i]. Title. Font. Name: =' song body '; //font
(Sender as TDBGrid). Columns[i]. Title. Font. Size: =9; //font size
(Sender as TDBGrid). Columns[i]. Title. Font. Color: =$000000ff; //Font Color (red)
(Sender as TDBGrid). Columns[i]. Title. Color: =$0000ff00; //Background color (green)
end;
Change the mesh background color interlaced:
if Query1. RecNo mod 2 = 0 Then
(Sender as TDBGrid). Canvas. Brush. Color: = CLINFOBK //define background color
Else
(Sender as TDBGrid). Canvas. Brush. Color: = RGB (191, 255, 223); //define background color
To define the color of the grid lines:
DBGrid1.  Defaultdrawcolumncell (rect,datacol,column,state);
With (Sender as TDBGrid). Canvas do //Draw the cell's border
begin
Pen. Color: = $00ff0000; //define Brush Color (blue)
MoveTo (Rect. Left, Rect. Bottom); //Brush positioning
LineTo (Rect. Right, Rect. Bottom); //Draw a blue horizontal Line
Pen. Color: = $0000ff00; //define Brush color (green)
MoveTo (Rect. Right, Rect. TOP); //Brush positioning
LineTo (Rect. Right, Rect. Bottom); //Draw a green vertical Line
end;
End;</span>














3, Vertical zebra crossing effect: The realization of the grid of odd sequence and even series of different colors display to distinguish adjacent data columns.





[Delphi]


Case Datacol Mod 2 = 0 of
True:dbgrid1. Canvas. Brush. Color:= Clblue; file://even sequence in blue
False:dbgrid1. Canvas. Brush. Color:= Claqua; file://Odd series with light green
End;
DbGrid1. Canvas. Pen.  Mode:=pmmask;
DBGrid1.  Defaultdrawcolumncell (rect,datacol,column,state);




4, vertical zebra crossing, while highlighting the current cell effect in red: To highlight the currently selected field.


[Delphi]

Case Datacol Mod 2 = 0 of
True:dbgrid1. Canvas. Brush. Color:= Clblue; file://even sequence in blue
False:dbgrid1. Canvas. Brush. Color:= Claqua; file://Odd series with light green
End;
If (state = [gdselected]) or (state=[gdselectedgdfocused] ) and then
If not DbGrid1. Selectedrows. currentrowselected Then
DbGrid1. Canvas. Brush. color:=clred; file://The currently selected cell shows Red
DbGrid1. Canvas. Pen.  Mode:=pmmask;
DBGrid1.  Defaultdrawcolumncell (rect,datacol,column,state);




5. Highlight the currently selected row in red in the data grid.








[Delphi]


Set the Dgrowselect property in the Options property of the DBGrid control to True, the Color property is Claqua (background color)
Write the following code in the DBGrid Drawcolumncell event:
if (state = [gdselected]) or (state=[gdselected gdfocused] ) Then
DbGrid1. Canvas. Brush. color:=clred; file://The current row is shown in red, the other row uses a light green background
DbGrid1.  Canvas. Pen. Mode:=pmmask;
DBGrid1.  Defaultdrawcolumncell (rect,datacol,column,state);





6. The Zebra effect: Highlight the current row, and distinguish between different columns (fields).








[Delphi]



if (state = [gdselected]) or (state=[gdselectedgdfocused] ) and then
Begin
Case Datacol Mod 2 = 0 of
True:dbgrid1. Canvas. Brush. color:=clred; file://The even column of the currently selected row shows Red
False:dbgrid1. Canvas. Brush. Color:=clblue; file://Odd column of the currently selected row shows Blue
End
DbGrid1.  Canvas. Pen. Mode:=pmmask;
DBGrid1.  Defaultdrawcolumncell (rect,datacol,column,state);




7, horizontal zebra crossing, at the same time in red to highlight the current line effect.








[Delphi]


Case Table1. RecNo mod 2 = 0 of the file://Judging by the record number of the DataSet
True:dbgrid1. Canvas. Brush. Color:=claqua; file://Even lines are shown in light green
False:dbgrid1. Canvas. Brush. Color:=clblue; file://odd lines are indicated in blue
End
if (state = [gdselected]) or (state=[gdselectedgdfocused]) and then file://Selected row is shown in red
DbGrid1. Canvas.  Brush. color:=clred;
DbGrid1.  Canvas. Pen. Mode:=pmmask;
DBGrid1.  Defaultdrawcolumncell (rect,datacol,column,state);


8, two-way zebra crossing effect: that is, between the lines with different colors, at the same time, the selection of rows with vertical zebra crossing effect to distinguish different columns.








[Delphi]

Case Table1. RecNo mod 2 = 0 of the file://Judging by the record number of the DataSet
True:dbgrid1. Canvas. Brush. Color:=claqua; file://Even lines are shown in light green
False:dbgrid1. Canvas. Brush. color:= clblue; file://odd lines are indicated in blue
End
If (state = [gdselected]) or (state=[gdselectedgdfocused] ) and then
Case Datacol mod 2 = 0 of
True:dbgrid1. Canvas. Brush. color:=clred; file://The even sequence of the currently selected row is in red
False:dbgrid1. Canvas. Brush. color:= Clgreen; file://The odd sequence of the currently selected row is indicated in green
End
DbGrid1.  Canvas. Pen. Mode:=pmmask;
DBGrid1.  Defaultdrawcolumncell (rect,datacol,column,state);




One, according to the conditions to change a flight



Procedure Tfrm_ticketget.dbgrid1drawcolumncell (Sender:tobject;
Const Rect:trect; Datacol:integer; Column:tcolumn;
State:tgriddrawstate);
Begin
if (Adoquery1.fieldbyname (' Kind_name '). asstring = ' total ') or (Adoquery1.fieldbyname (' Kind_name '). asstring = ' Total Total ') then
Begin
dbgrid1.canvas.font.color:=clred;
Dbgrid1. Canvas.brush.color:=clyellow;
End;

Dbgrid1. Defaultdrawcolumncell (rect,datacol,column,state);
End






Delphi Change DBGrid Color Tips


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.