Search code examples
delphidelphi-xe3firemonkey-fm2

How to prevent dragging columns on a Firemonkey grid?


Is there a way to prevent column header dragging (re-ordering) on a FireMonkey 2 TGrid?

The THeader instance is private to the TGrid class, and I can't find any other property that controls this. The default of THeaderItem.DragMode is dmAutomatic (for some unfathomable reason).

Short of creating a new TGrid descendant, or breaking the THeader out of the grid, I can't see how to get DragMode back to dmManual.


Solution

  • You can access the FHeader private field using the RTTI, and from here you can write a method to set the DragMode of the Headers.

    Try this code

    procedure SetHeaderDragMode(Grid : TGrid; DragMode:TDragMode);
    Var
      LCtx  : TRttiContext;
      LType : TRttiType;
      LField: TRttiField;
      LHeader: FMX.Grid.THeader;
      i : Integer;
    begin
      LCtx:=TRttiContext.Create;
      LType:=LCtx.FindType('FMX.Grid.TGrid');
      if (LType<>nil) then
       LField:=  LType.GetField('FHeader');
    
      if (LField<>nil) then
      begin
        LHeader:=THeader(LField.GetValue(Grid).AsObject);
        if LHeader<>nil then
          for i:=0 to  LHeader.Count-1 do
            LHeader.Items[i].DragMode:=DragMode;
      end;
    end;
    

    And use like so

     SetHeaderDragMode(Grid1,TDragMode.dmManual);