Jump to content
Tom F

How to change background color in a TDbGrid column editor?

Recommended Posts

tl;dr: How can I configure a column in the TDbGrid (or TRzDbGrid if it's easier) so that when the user clicks in a cell, I get control to set the cell's background color? 

Details:

I am using a TDbGrid that has a column for the user to select a color that is then used elsewhere in the application.

 

There is no text in the cells in the column, just a color.

 

In DrawColumnCell, for that column, I set Canvas.Brush.Color so that the cell displays a previously selected color.

 

This column has ButtonStyle := cbsEllipsis;

 

When the user clicks the ellipsis, I popup a TColorDialog for the user to choose a color. 

 

The above is all working fine, except when the user clicks in the cell (before clicking the ellipsis) the Brush color goes back to white. (See attached image.) I don't want to auto-open the color picker.

 

I believe setting the editor color involves cracking the InplaceEditor but I haven't been able to find enough info online to figure out how to do that.

 

cell.jpg

Share this post


Link to post

One way I found to achieve this is to use the fact that in the TCustomGrid.UpdateEdit procedure UpdateEditor will set the color of the editor to match the color of the grid:

 

type TCrackGrid = class(TDBGrid);

procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
  UpdateEditorColor
end;

procedure TForm1.FDQuery1AfterScroll(DataSet: TDataSet);
begin
  UpdateEditorColor;
end;

procedure TForm1.UpdateEditorColor;
begin
    if dbgrid1.SelectedField = fdQuery1Color then
    begin // Here is the code to setup the color of the inplace editor.....
      if odd(FDQuery1.RecNo) then
        dbgrid1.Color:=clRed
      else
        dbgrid1.Color:=clWhite;
   end
   else
     dbgrid1.Color:=clWhite;

    TCrackGrid(DBGrid1).InvalidateEditor;
end;

 

Share this post


Link to post
Guest

one note:

  • Using the property "RECNO () -> by DataSet", we are at the mercy of the record value in the table, however, if we use a filter in the table, we will have a problem with even and odd values, that is, many records in the table filtered can be odd or even even records ...
  • So, the most correct thing would be to use the property "ROW -> propriety protected on ancestral class", however, in the most traditional way and found on the internet, the Grid will be crazy, crazy, crazy .... because the event above is called numerous times and the property receives various values at this stage.
  • but, if not using a "Filter or similar" all works!

hug

Edited by Guest

Share this post


Link to post

Thanks, Lajos. That worked great.

For others reading this in the future, I also just found this similar approach: https://www.swissdelphicenter.ch/en/showcode.php?id=1113

emailx45: You're right, of course, that using .Recno() fails if there is filtering in place.  I think Lajos was just using that as his example to show how we can control color on a record by record basis. Thanks for your post.

Tom
 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×