tikskit
tikskit

Reputation: 339

How to control cell's width from OnCustomDrawCell event handler?

I use OnCustomDrawCell event handler to draw bitmaps and text in cells. Calling ApplyBestFit makes width of the cells enought to fit bitmaps but not enought to fit text.

For example a width of a bitmap is 16, width of a text is 100. After calling ApplyBestFit width of a cell will be 100 (corresponding to the width of the text) and I'll see the bitmap and a part (84px) of the text.

I would like to the width of the cell be 116 in order to fit both the bitmap and the text.

Is it possible to set desired width of the cell within the OnCustomDrawCell event handler?

Upvotes: 0

Views: 719

Answers (2)

bummi
bummi

Reputation: 27384

You could override TcxCustomGridTableItem.CalculateBestFitWidth and add the needed space to the result of the function. Inthe example below the derived class TcxGridDBColumn is used. If you are using a TcxGridTableView you will have to interpose TcxGridColumn, if you are using TcxGridDBBandedTableView you will have to interpose TcxGridDBBandedColumn and so on ...
EDIT
As correctly mentioned by @tikskit, if you are needed to create columns at runtime (e.g. by calling View.DataController.CreateAllItems()) it's necessary to override GetItemClass of the used view's class so that the interposed class will be created.

type
  TcxGridDBTableView=Class(cxGridDBTableView.TcxGridDBTableView)
    function GetItemClass: TcxCustomGridTableItemClass; override;
  End;

  TcxGridDBColumn=Class(cxGridDBTableView.TcxGridDBColumn)
     function CalculateBestFitWidth: Integer;override;
  End;

  TForm3 = class(TForm)
    v: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    vID: TcxGridDBColumn;
    vName: TcxGridDBColumn;
    .....
    .....
var
  Form3: TForm3;

implementation

{$R *.dfm}

{ TcxGridDBColumn }

function TcxGridDBColumn.CalculateBestFitWidth: Integer;
begin
  Result := inherited CalculateBestFitWidth;
  Result := Result + 16;
end;

{ TcxGridDBTableView }


function TcxGridDBTableView.GetItemClass: TcxCustomGridTableItemClass;
begin
   inherited;
  Result := TcxGridDBColumn;
end;

Upvotes: 1

Agustin Seifert
Agustin Seifert

Reputation: 1968

Declare an access class to TcxCustomGridTableItem:

type
  TcxCustomGridTableItemAccess = class(TcxCustomGridTableItem);

and in your method do:

procedure Test.cxGrid1DBTableView1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if Assigned(AViewInfo) and Assigned(AViewInfo.Item) then
    TcxCustomGridTableItemAccess(AViewInfo.Item).Width := 116; // or calc here your new width
end;

Upvotes: 0

Related Questions