2

When the user selects the header of my grid, I would like to bold the column header of the grid.
I use the following code to achieve this:

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var I: Integer;
begin
  //Prevent multiple clicks!  
  if fsBold in Column.Title.Font.Style then
    Exit;

  for I:= 0 to Column.Collection.Count-1 do
    (Column.Collection.Items[I] as TColumn)
            .Title.Font.Style := [];
  Column.Title.Font.Style := [fsBold];
end;

This code is working fine.

In the OnFormCreate I've defined the following header height:

type
  DBGridHack = class(TDBGrid);

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGridHack(DBGrid1).RowHeights[0] := 45;
end;

When I click in the Title, the procedure InternalLayout.MeasureTitleHeights changes the size back to the original. Thus, every time I change the font style I need to redefine the value for RowHeight[0].

The code would look like this:

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var I: Integer;
begin
  //Prevent multiple clicks!  
  if fsBold in Column.Title.Font.Style then
    Exit;

  for I:= 0 to Column.Collection.Count-1 do
    (Column.Collection.Items[I] as TColumn)
            .Title.Font.Style := [];
  Column.Title.Font.Style := [fsBold];

  //Every time!!
  DBGridHack(DBGrid1).RowHeights[0] := 45;
end;

There is another issue when the user redefine the Column's width, in this case the RowHeight[0] also returns to the original value. The only way I imagine to solve this, is to inherit DBGrid and override ColWidthsChanged, but how much more methods will I have to override?

Why this happens?
Is this a bug, or is my code bugged?
Is there a way to workaround this?

In a simple question.

How to FIX a value to the DBGrid's header heigth?

EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59

1 Answers1

0

It seems like if I override the Paint procedure, it will work as expected. I don't know if there is side effects about this solution.

This is the code for the custom DBGrid. Please if this is not a good solution I would appreciate if you state it so.

TDBGrid1 = class(TDBGrid)
  private
    FTitleFixedHeight: Integer;
    function CheckHeaderFixed: Boolean;
  protected
    procedure Paint; override;
  public
    { Public declarations }
  published
    property TitleFixedHeight: Integer read FTitleFixedHeight write FTitleFixedHeight;
  end;

function TDBGrid1.CheckHeaderFixed: Boolean;
begin
  Result := (TitleFixedHeight <> 0) and (RowHeights[0] <> TitleFixedHeight);
end;

procedure TDBGrid1.Paint;
begin
  if CheckHeaderFixed then
  begin
    RowHeights[0] := TitleFixedHeight;
  end;    
  inherited;
end;
EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59