Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Coloring DBGrid's active cell's background



Permlink Replies: 6 - Last Post: Jul 21, 2017 11:34 AM Last Post By: Melissa Torn
Melissa Torn

Posts: 143
Registered: 4/30/09
Coloring DBGrid's active cell's background
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 19, 2017 5:28 AM
How can i change a dbgrid's active cell's background color ?
When user move to a new cell (with mouse or Tab key )
Prior active cell will be clWhite the new cell will be yellow.

Thank You
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Coloring DBGrid's active cell's background
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 19, 2017 7:33 AM   in response to: Melissa Torn in response to: Melissa Torn
procedure TfrmFireDacTest.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var TextColor, BackColor: TColor;
begin
  if (gdFocused in State) or (gdSelected in State) then
  begin
    TextColor := clBlack;
    BackColor := clYellow;
  end
  else
  begin
    TextColor := clBlack;
    BackColor := clWhite;
  end;
 
  DBGrid1.Canvas.Font.Color := TextColor;
  DBGrid1.Canvas.Brush.Color := BackColor;
  DBGrid1.Canvas.FillRect(Rect);
  DBGrid1.Canvas.TextOut(Rect.Left + 20, Rect.Top + 2, Column.Field.AsString);
end;


Maybe you have to play with "DefaultDrawing" property (true/false)
Melissa Torn

Posts: 143
Registered: 4/30/09
Re: Coloring DBGrid's active cell's background
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 19, 2017 9:15 AM   in response to: Robert Triest in response to: Robert Triest
Thank You Robert,
I am getting Access Violation error. msg.in below code
DBGrid1.Canvas.TextOut(Rect.Left + 20, Rect.Top + 2, Column.Field.AsString);
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Coloring DBGrid's active cell's background
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2017 2:45 AM   in response to: Melissa Torn in response to: Melissa Torn
I am getting Access Violation error. msg.in below code
DBGrid1.Canvas.TextOut(Rect.Left + 20, Rect.Top + 2, Column.Field.AsString);

And your Grid also is named as DBGrid1?
What version of Delphi are you using (I tested it on Tokio 10.2 and XE8)
Maybe it violates when there is no data in the grid?

Check/debug the value of Column.Field and Column.Field.AsString
Maybe Column.Field is nil?

if Column.Field<>nil then
begin
  if (gdFocused in State) or (gdSelected in State) then
  begin
    TextColor := clBlack;
    BackColor := clYellow;
  end
  else
  begin
    TextColor := clBlack;
    BackColor := clWhite;
  end;
 
  DBGrid1.Canvas.Font.Color := TextColor;
  DBGrid1.Canvas.Brush.Color := BackColor;
  DBGrid1.Canvas.FillRect(Rect);
  DBGrid1.Canvas.TextOut(Rect.Left + 20, Rect.Top + 2, Column.Field.AsString);
end;


Edited by: Robert Triest on Jul 20, 2017 11:45 AM
Melissa Torn

Posts: 143
Registered: 4/30/09
Re: Coloring DBGrid's active cell's background
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2017 4:44 AM   in response to: Robert Triest in response to: Robert Triest
Thank you so much Robert,

After adding if Colum <> Nil then
i dont get any error msg. but
as soon as i show the form i see yellow back color
in the most left of the dbgrid which is OK.
but when i move to the next cell with TAB key or Mouse
there is no yellow back color.( Both in XE & Berlin )

My Grid=DBGrid1, I use XE and Berlin
There are always data in the dbgrid.
I change DefaultDrawing True or False
nothing changes.

Again Thank you so much
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Coloring DBGrid's active cell's background
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2017 6:27 AM   in response to: Melissa Torn in response to: Melissa Torn
Can you test the source from the Attachment Forum I posted for you?

"DBGrid Color Background Cell Test"

https://forums.embarcadero.com/thread.jspa?threadID=253205&stqc=true

Note:
The DBGrid has two Draw events:

1)DBGrid1DrawColumnCell
2)DBGrid1DrawCell

You have to use 1)

Edited by: Robert Triest on Jul 20, 2017 3:30 PM
Melissa Torn

Posts: 143
Registered: 4/30/09
Re: Coloring DBGrid's active cell's background
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 21, 2017 11:34 AM   in response to: Robert Triest in response to: Robert Triest
Thank you so much Robert
i solved the problem thanks to your sample app.
I appreciate your help
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02