Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to coloring Cells in a StringGrid / DBGrid Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
17-Jul-03
Category
VCL-General
Language
Delphi 2.x
Views
143
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Alex Schlecht

StringGrids / DBGrids with colored cells looks very nice and you can inform the 
user about importent data inside the Grid.

Answer:

Unfortunately you can't use the same method for coloring StringGrids and DBGrids. 
So first let's have a look to the StringGrid: 

1. StringGrid 

Use the "OnDrawCell"-event to make your StringGrids colorful! The following Code 
shows how to give your Grid a red background color. The second column will be 
colored with green background. 
1   
2   procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
3     Rect: TRect; State: TGridDrawState);
4   
5   const //define your color here. Of course you
6     //can use default colors too.
7     clPaleGreen = TColor($CCFFCC);
8     clPaleRed = TColor($CCCCFF);
9   
10  begin
11  
12    //Does the cell have the focus you have to paint it with other colors
13    if (gdFocused in State) then
14    begin
15      StringGrid1.Canvas.Brush.Color := clBlack;
16      StringGrid1.Canvas.Font.Color := clWhite;
17    end
18    else //Does the cell have NOT the focus you can use
19      //your personal colors here
20      if ACol = 2 //the second Column should be
21      {//green, the other cells red } then
22        StringGrid1.Canvas.Brush.color := clPaleGreen
23      else
24        StringGrid1.canvas.brush.Color := clPaleRed;
25  
26    //Now Paint the cells, but only, if the cell isn't the Title- Row/Column
27    //This of course depends whether you have title-Row/Columns or not.
28  
29    if (ACol > 0) and (ARow > 0) then
30    begin
31      //Painting the Background
32      StringGrid1.canvas.fillRect(Rect);
33  
34      //Painting the Text. Here you can improve the code with
35      // using alignment and so on.
36      StringGrid1.canvas.TextOut(Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]);
37    end;
38  end;


If you want to colorize your cells depending on values in the cells you can replace 
the 3 lines (if Acol = 2 ......) with something like this 

39  if StringGrid1.Cells[ACol, ARow] = 'highlight it' then
40    StringGrid1.Canvas.Brush.color := clPalered
41  else
42    StringGrid1.canvas.brush.Color := clwhite;


But now lets coloring DBGrids: 

2. DBGrid 

It's much easier to give color to DBGrids. Here you have to use the 
"OnDrawColumnCell"-Event. The following example is coloring the Cells of Column 
"Status" when the value is not "a". 
If you want to color the whole line you only have to delete the "If..." statement 
(see below) 
43  
44  procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
45    DataCol: Integer; Column: TColumn;
46    State: TGridDrawState);
47  const
48    clPaleGreen = TColor($CCFFCC);
49    clPaleRed = TColor($CCCCFF);
50  begin
51  
52    if Column.FieldName = 'Status' then //Remove this line, if you want
53      //to highlight the whole line
54  
55      if Column.Field.Dataset.FieldbyName('Status').AsString <> 'a' then
56        if (gdFocused in State) {//does the cell have the focus? } then
57          dbgrid1.canvas.brush.color := clBlack //focused
58        else
59          dbgrid1.canvas.brush.color := clPaleGreen; //not focused
60  
61    //Now let's paint the cell using a Default-Method:
62    dbgrid1.DefaultDrawColumnCell(rect, DataCol, Column, State)
63  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC