'Hello' from FMX TStringGrid, with style (TStyledGrid):

'Hello' from FMX TStringGrid, with style (TStyledGrid):

procedure TForm3.StringGrid1EditingDone(
Sender: TObject; const ACol, ARow: Integer);
begin
StringGrid1.Model.EditorMode := False;

if StringGrid1.Model.EditorMode then
Caption := 'Hello!'
else
Caption := 'Foo';
end;

(see details in a comment.)

Comments

  1. Details:

    FMX TStringGrid, 10.1 Berlin Update 2
    2 StringColumns, one of them readonly
    TGridOption.MoveColumn removed
    TGridOption.AlwaysShowEditor removed
    OnEditingDone handled

    object StringGrid1: TStringGrid
    //...
    RowCount = 6
    Options = [Editing, ColumnResize, ColLines, RowLines, Tabs, Header, HeaderClick, AutoDisplacement]
    OnEditingDone = StringGrid1EditingDone
    object StringColumn1: TStringColumn
    Header = 'Col1'
    ReadOnly = True
    end
    object StringColumn2: TStringColumn
    Header = 'Col2'
    end
    end

    Observations:

    1) Columns can be moved.

    2) F2 in ReadOnly Column shows Editor.

    3) EditorMode is True in OnEditingDone EventHandler.

    4) EditorMode cannot be set to False in the EventHandler.

    These are the 'basic issues' 1-4.

    'Advanced' stuff below:

    TDisplayGrid = class(TStringGrid);
    TDisplayGridColumn = class(TColumn)
    protected
    procedure BeforeDrawing(const Canvas: TCanvas); override;
    procedure DrawBackground(const Canvas: TCanvas; const Bounds: TRectF;
    const Row: Integer; const Value: TValue; const State: TGridDrawStates); override;
    public
    [Weak] DisplayGrid: TDisplayGrid;
    Tal: TTextAlign;
    Col: Integer;
    constructor Create(AOwner: TComponent); override;
    end;

    5) Where to specify the text align for the Column

    procedure TDisplayGridColumn.BeforeDrawing(const Canvas: TCanvas);
    begin
    if Layout = nil then
    inherited; //need to create it

    Layout.BeginUpdate;
    try
    Layout.HorizontalAlign := Tal;
    finally
    Layout.EndUpdate;
    end;
    end;

    Layout is initially nil, you cannot assign to Layout, but set properties.

    6) Where to specify the background color of individual cells

    procedure TDisplayGridColumn.DrawBackground(
    const Canvas: TCanvas;
    const Bounds: TRectF;
    const Row: Integer;
    const Value: TValue;
    const State: TGridDrawStates);
    var
    cp: TCellProp;
    begin
    if not Released and (Model <> nil) then
    begin
    cp := DisplayGrid.ColGrid.CellProps.CellProp[Col, Row];
    Canvas.Fill.Assign(ColorCache.GetBrush(cp.Color)); // <--
    Canvas.FillRect(Bounds, 0, 0, AllCorners, AbsoluteOpacity);
    end;
    end;

    7) How to tell Delphi what Presentation to use for your CustomGrid

    TPresentationProxyFactory.Current.Register(TDisplayGrid,
    TControlType.Styled, TStyledPresentationProxy);

    See section initialization, finalization in FMX.Grid.Style
    See procedure TCustomGrid.AfterPaint; in FMX.Grids

    ReplyDelete
  2. When editing finished, important stuff happens, content of other grid cells will change! It is a good thing that the Grid caches the strings for its cells. New data will only be pushed to the Grid if EditMode is False, ok? That is how it used to be. Have to use a special Flag, because they are consuming EditMode for internal purposes too much now.

    ReplyDelete
  3. The DrawBackground override of the Column is effective only if TGridOption.AlternatingRowBackground is included in Model.Options. (You need to know that as well.)

    ReplyDelete

Post a Comment