Re: Color row of DBGrid



I notice the proc you wrote applies to a TStingGrid, rather then to a
DBGrid. How do I replace the "DrawCell(ACol, ARow, Rect, State);" statement
since the OnDraw event of a DBGrid supplies Columns coordinates rather then
Cells ?

"Fons" <fonzzzNO@xxxxxxxxxxxxxxxxxxx> ha scritto nel messaggio
news:468b9397$0$323$e4fe514c@xxxxxxxxxxxxxxxxx
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
    ... I'll try to make a DBGrid example this afternoon and tell you this evening. ... How do I replace the "DrawCell(ACol, ARow, Rect, State);" statement ... 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, ...
    (comp.lang.pascal.delphi.misc)
  • ANN: TAdvStringGrid v3.0 released
    ... We're excited to announce the release of the new TAdvStringGrid v3.0 at ... copies grid as HTML on the clipboard ... Support for background display over full background, ... Filter selection to filter on virtual cell text or grid cells text ...
    (borland.public.delphi.thirdpartytools.general)
  • ANN: GridPack V2.0 for ReportBuilder - now better than ever !!!
    ... package: TrbCrossTab and TrbPageBreak. ... Grid Pack for ReportBuilder is a set of components designed ... to work with ReportBuilder, the report tool from Digital Metaphors, ... - Cells can be stretch. ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: GridPack V2.0 for ReportBuilder - now better than ever !!!
    ... > to work with ReportBuilder, the report tool from Digital Metaphors, ... > TrbTableGrid is a stretchable table grid, ... > - Cells can be merged and split. ... > - All the features of TrbTableGrid, plus, ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Dungeon via Particle Aggregation Algo
    ... Draw a corridor between one of the existing rooms and selected cell ... If for example, you draw a room, and then use that room as the next center point, you would probably end up with a winding passage of rooms resembling a random walk along the grid. ... This method, as well as the bsp method, could be considered cousins in this respect, in that both are working within a grid of cells to define the working space of the dungeon. ... On a theme level you may have a central temple, a secondary temple, a hidden treasure room, some crypts, a prayer room, etc. ...
    (rec.games.roguelike.development)