Board index » delphi » Help, Please : Changing Color of DBGrid Component

Help, Please : Changing Color of DBGrid Component

I am trying to simulate multiple selection with a DBGrid and I need to
know how to change the color of selected grid cells.

If any body knows how to do this could you please email me if it is
not to much trouble.

Michael.
===================================================
http://www-mugc.cc.monash.edu.au/~mrtoe1
email - mrt...@mugca.cc.monash.edu.au
===================================================

 

Re:Help, Please : Changing Color of DBGrid Component


Quote
mrt...@mugca.cc.monash.edu.au wrote:

> I am trying to simulate multiple selection with a DBGrid and I need to
> know how to change the color of selected grid cells.

> If any body knows how to do this could you please email me if it is
> not to much trouble.

> Michael.
> ===================================================
> http://www-mugc.cc.monash.edu.au/~mrtoe1
> email - mrt...@mugca.cc.monash.edu.au
> ===================================================

Michael,

        Hi.  U have to place the color changing code in the OnDrawDataCell event of your grid.  Then u
must call the DefaultDrawDataCell command for the color to actually take effect.  Here's
a piece of code which changes the color of text appearing in a DBGrid to red:

        DBGrid1.Canvas.Font.Color := clRed;
        DBGrid1.DefaultDrawDataCell(Rect, Field, State);

Good luck,
Ali

Re:Help, Please : Changing Color of DBGrid Component


Quote
mrt...@mugca.cc.monash.edu.au wrote:
>I am trying to simulate multiple selection with a DBGrid and I need to
>know how to change the color of selected grid cells.

>If any body knows how to do this could you please email me if it is
>not to much trouble.

>Michael.
>===================================================
>http://www-mugc.cc.monash.edu.au/~mrtoe1
>email - mrt...@mugca.cc.monash.edu.au
>===================================================

Play around with this.
Obviously there are different ways that the selection tests can be
done.

procedure TfmDesktop.DBGrid1DrawDataCell(Sender: TObject;
  const Rect: TRect; Field: TField; State: TGridDrawState);
begin
  { this will make all cells in the first column green }
  if (Field.Index=0) then begin
    DBGrid1.Canvas.Font.Color := clGreen;
    DBGrid1.DefaultDrawDataCell(Rect, Field, State);
  end;
  { this will display any cell that contains the text 'PAYE' in blue }
  if (Field.DisplayText='PAYE') then begin
    DBGrid1.Canvas.Font.Color := clBlue;
    DBGrid1.DefaultDrawDataCell(Rect, Field, State);
  end;
  { this will display all the cells in a row red }
  { if certain conditions are true in the record }
  { STAC is the data field name  }
  if (Query1STAC.AsString='CLOSED') then begin
    DBGrid1.Canvas.Font.Color := clRed;
    DBGrid1.DefaultDrawDataCell(Rect, Field, State);
  end;
end;

==
Bob Small

Re:Help, Please : Changing Color of DBGrid Component


Quote
StarCrusher <abukh...@adam.cc.sunysb.edu> wrote:
>mrt...@mugca.cc.monash.edu.au wrote:

>> I am trying to simulate multiple selection with a DBGrid and I need to
>> know how to change the color of selected grid cells.

>> If any body knows how to do this could you please email me if it is
>> not to much trouble.

>> Michael.
>> ===================================================
>> http://www-mugc.cc.monash.edu.au/~mrtoe1
>> email - mrt...@mugca.cc.monash.edu.au
>> ===================================================
>Michael,
>    Hi.  U have to place the color changing code in the OnDrawDataCell event of your grid.  Then u
>must call the DefaultDrawDataCell command for the color to actually take effect.  Here's
>a piece of code which changes the color of text appearing in a DBGrid to red:
>    DBGrid1.Canvas.Font.Color := clRed;
>        DBGrid1.DefaultDrawDataCell(Rect, Field, State);

Note that the Delphi 2.0 DBGrid already supports multiselected rows.

-Danny

Re:Help, Please : Changing Color of DBGrid Component


Quote
lur...@subliminal.com wrote:

> StarCrusher <abukh...@adam.cc.sunysb.edu> wrote:

> >mrt...@mugca.cc.monash.edu.au wrote:

> >> I am trying to simulate multiple selection with a DBGrid and I need to
> >> know how to change the color of selected grid cells.

> >> If any body knows how to do this could you please email me if it is
> >> not to much trouble.

> >> Michael.
> >> ===================================================
> >> http://www-mugc.cc.monash.edu.au/~mrtoe1
> >> email - mrt...@mugca.cc.monash.edu.au
> >> ===================================================

> >Michael,

> >       Hi.  U have to place the color changing code in the OnDrawDataCell event of your grid.  Then u
> >must call the DefaultDrawDataCell command for the color to actually take effect.  Here's
> >a piece of code which changes the color of text appearing in a DBGrid to red:

> >       DBGrid1.Canvas.Font.Color := clRed;
> >        DBGrid1.DefaultDrawDataCell(Rect, Field, State);

> Note that the Delphi 2.0 DBGrid already supports multiselected rows.

> -Danny

Note also that in Delphi 2.0 you have to make your changes into
DefaultDrawDataColumn event instead of the ..DataCell counterpart in
Delphi 1.0

E. Martinez

Other Threads