Re: Color row of DBGrid



Thanks, I'all surely try your code.

"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
    ... StringGrid.Canvas.TextHeight) div 2; ... DrawCell is called for *every* cell. ... TextTop and TextLeft are only to center the text. ...
    (comp.lang.pascal.delphi.misc)
  • InnerHTML drops leading tag in SELECT
    ... I have the DIV and SPAN changes working well but I ... In other words, the string ... but it still strips off the leading OPTION tag when I pop the SELECT ...
    (microsoft.public.dotnet.framework.aspnet)
  • Javascript printing question? Using C# , ASP.NET & Oracle
    ... This is a javascript is not being used. ... I just want to print the string. ... <DIV> ... OdbcDataAdapter(strSQLDDL, myConnection); ...
    (microsoft.public.dotnet.languages.csharp)
  • Creating a dynamic file link
    ... item was contained in it's own hidden <div>. ... Dim fn As String = Request.QueryString ... Dim fp As StreamReader ... the variable udf causes the test.txt file to be ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Creating a dynamic file link
    ... In the past I had an HTML page that contained a lot of code items and each ... item was contained in it's own hidden <div>. ... string to load the contents of a text file into a div. ... Dim fp As StreamReader ...
    (microsoft.public.dotnet.framework.aspnet)