Re: Color row of DBGrid



Try this:

procedure TFormMain.StringGridDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
DrawCell(ACol, ARow, Rect, State);
end;

procedure TFormMain.DrawCell(ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
S : string;
TextTop, TextLeft : integer;
begin
S := Trim(StringGrid.Cells[ACol, ARow]);

TextTop := Rect.Top + (RectHeight(Rect) - StringGrid.Canvas.TextHeight(S)) div 2;
TextLeft := Rect.Left + (RectWidth(Rect) - StringGrid.Canvas.TextWidth(S)) div 2;

StringGrid.Canvas.Font.Color := clRed;
StringGrid.Canvas.Pen.Color := clRed;
StringGrid.Canvas.Brush.Color := clBtnFace;
StringGrid.Canvas.TextRect(Rect, TextLeft, TextTop, S);
end;

DrawCell is called for *every* cell (you can put the code directly in StringGridDrawCell, but I always try to keep the code in the event-handler as short as possible). But in DrawCell you can change the Font, Pen or Brush color depending S (the text in the cell) or the coordinates. You can draw lines, draw circles etc. But *do not* draw outside the Rect. If you do you'll mess it up. Try drawing with random colors. You'll see, this is not what you want. It'll give some kaleidoscope when resizing the grid.

TextTop and TextLeft are only to center the text.

Hope this helps,
Fons.

Would you share what you have done or give guidance on how to do it ?

"Fons" <fonzzzNO@xxxxxxxxxxxxxxxxxxx> ha scritto nel messaggio
news:468ab710$0$336$e4fe514c@xxxxxxxxxxxxxxxxx
I change color of the 'current' record in a DBGrid using the
OnDrawColumnCell eventi with the following code:

if (gdFocused in State) or (gdSelected in State) then
begin
if BtnMod.Down then
MyDBGrid.Canvas.Brush.Color := clYellow
else
MyDBGrid.Options := DelegheDBGrid.Options + [dgRowSelect];
MyDBGrid.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;

The problem I have is that it doesn't work exactly as I wanted. The
first
time I show the DBGrid no record are hilighted though dgRowSelect is
true.
Clicking on the BtnMod the focused row is correctly painted in yellow,
since
dgRowSelect is true. But I need dgRowSelect to be false to edit the
record
and if it is set to false, the cells are not painted in yellow.
I think you should do ALL the drawing of the cells in OnDrawColumnCell.
I have written a program with a grid with user-chosen colors, crosses in
colored cells etc. Most of the standard drawing, not different from a
standard grid is simple; that's not too much work.

I think it doesn't work with just changing the Brush.Color and the
Options.
Thanks in advance for any suggestions.
Fons.


.



Relevant Pages

  • Re: Color row of DBGrid
    ... S: string; ... StringGrid.Canvas.TextHeight) div 2; ... TextTop and TextLeft are only to center the text. ...
    (comp.lang.pascal.delphi.misc)
  • div inside
(again)
... moved down the page a bit, and so has the div. ... The table cell doesn't ... and the width seemed to have been inherited from the viewport, ... which seems to inherit the height and width from the ...
(comp.infosystems.www.authoring.stylesheets)
  • Re: CSS: formatting tables for screen and handheld
    ... they won't fit, then do something else"? ... each cell as a separate <div> and use CSS to make these float ... handling images), to set the width of all the cell s to be the ...
    (comp.infosystems.www.authoring.stylesheets)
  • Re: Text wrapped around a picture -- picture longer than text
    ... No, it's strictly so that <div> specifies a block, and this means line ... The reason I say it's more like a paragraph separator than a line-break ... The CSS spec says that a table cell is a "block formatting context". ...
    (comp.infosystems.www.authoring.html)
  • Re: More Questions-Re: Absolute Position of a table realtive to a cell
    ... This provides code for very old browsers. ... Placing the image inside a relatively positioned container (<div ... The div fills the table cell, so the cords required are top: 0; right: 0; to place the image in top right of the and thus table cell. ... >> absolute position, but I was wrong. ...
    (microsoft.public.frontpage.programming)