Re: Color row of DBGrid
- From: Fons <fonzzzNO@xxxxxxxxxxxxxxxxxxx>
- Date: Wed, 04 Jul 2007 14:34:23 +0200
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
firstI 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. Thetrue.time I show the DBGrid no record are hilighted though dgRowSelect issinceClicking on the BtnMod the focused row is correctly painted in yellow,recorddgRowSelect is true. But I need dgRowSelect to be false to edit theOptions.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 theThanks in advance for any suggestions.Fons.
- Follow-Ups:
- Re: Color row of DBGrid
- From: FrancoJommi
- Re: Color row of DBGrid
- From: FrancoJommi
- Re: Color row of DBGrid
- References:
- Re: Color row of DBGrid
- From: Fons
- Re: Color row of DBGrid
- From: FrancoJommi
- Re: Color row of DBGrid
- Prev by Date: Re: Color row of DBGrid
- Next by Date: Re: Working with windows handles
- Previous by thread: Re: Color row of DBGrid
- Next by thread: Re: Color row of DBGrid
- Index(es):
Relevant Pages
|