[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Change color of a grid row

Using Delphi 3 it is quite easy to change the color of a column but I can't see how to changed the color of a row (or even just a single cell, for that matter) . My main interest is a dbgrid but I guess the same principle applies to a stringgrid.

For example I have a customer DBgrid that I would like to highlight all customers over 30 days due, in the color red
0
clyde99
Asked:
clyde99
  • 4
  • 2
1 Solution
 
intheCommented:
hi,
i give you 2 examples of drawing the grids(yes i have the samples in stringgrids but also applies to dbgrids :-)

example 1:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  FocusedRect : TRect;
begin
  FocusedRect := Rect;
  StringGrid1.Canvas.Font.Color  := clBlack;
  StringGrid1.Canvas.Pen.Color := clRed;
  StringGrid1.Canvas.Pen.Width := 1;
  if ( gdFocused in State ) then begin
    StringGrid1.Canvas.Font.Style  := [fsBold];
    StringGrid1.Canvas.Font.Color  := clBlue;
    StringGrid1.Canvas.Brush.Color  := clRed;
  end
else
    StringGrid1.Canvas.Font.Style  := [];
  InflateRect(FocusedRect, -2, -2);
  StringGrid1.Canvas.FillRect(FocusedRect);
   if ( gdSelected in State ) then
    StringGrid1.Canvas.TextOut(Rect.Left+8, Rect.Top+2, StringGrid1.Cells[ACol,ARow])   else
    StringGrid1.Canvas.TextOut(Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol,ARow])
 end;


example 2:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
 checked: boolean;
 oldbrush: integer;
 oldpen: tpen;
begin
 with stringgrid1.canvas,rect do
 begin
  oldbrush:=brush.color;
  brush.color:=clyellow;
  oldpen:=pen;
  pen.Width:=2;
  pen.color:=clred;
  moveto(left+1,top+2);
  polygon([point(left,top),
           point(left,bottom),
           point(right,bottom),
           point(right,top),
           point(left,top)]);
  checked:=boolean(stringgrid1.Objects[acol,arow]);
  if checked then
  begin
  brush.Color := clblue;
  polygon([point(left,top),
           point(left,bottom),
           point(right,bottom),
           point(right,top),
           point(left,top)]);
  end;
  brush.color:=oldbrush;
  textout(left+17,top+4,stringgrid1.cells[acol,arow]);
  pen:=oldpen;
 end;
end;

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
 r,c: integer;
 checked: boolean;
begin
 with stringgrid1 do
 begin
  mousetocell(x,y,c,r);
  checked:=(longint(objects[c,r])<>0);
  if checked then objects[c,r]:=pointer(0) else objects[c,r]:=pointer(1);
 end;
 end;


should give some ideas
Regards Barry
0
 
intheCommented:
btw.
for example2 set the defaultdrawing to false in the object inspector.
0
 
clyde99Author Commented:
The above code for example 1 only seems to work when I select a cell with the mouse. How do I get the the required cells/rows to change color when the grid is initially displayed?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
intheCommented:
hi,
try this :
I created a form, dropped a TTable component it, and pointed it to the EMPLOYEE.DB database in the DBDEMOS database. I dropped a Datasource and DBGrid on the form so that it showed on the form.
then set the code of the form to be like:


var
  Form1: TForm1;
    SOMEDate : TDate;
implementation

{$R *.DFM}

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  holdColor: TColor;
begin
  holdColor := DBGrid1.Canvas.Brush.Color;
  if Column.FieldName = 'HireDate' then
     if (Column.Field.AsDateTime < SOMEDate )then begin
      DBGrid1.Canvas.Brush.Color := clred;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      DBGrid1.Canvas.Brush.Color := holdColor;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 SOMEDate := Strtodate('01/06/1992');
end;


is that better?
Regards Barry
0
 
clyde99Author Commented:
Barry,

Thankyou.

That was EXACTLY what I was trying to do

Clyde
0
 
intheCommented:
good to hear &     :-))
merry xmas
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now