FMX TGrid OnGetValue after column moving

不问归期 提交于 2019-12-25 09:03:51

问题


I have Grid with enabled columns moving, and code:

type
  TRec = record
    Col0,
    Col1,
    Col2: string;
  end;

var
  Data: TArray<TRec>;

procedure TFormMain.GridGetValue(Sender: TObject; const Col, Row: Integer; var Value: TValue);
begin
  case Col of
    0:  Value := Data[Row].Col0;
    1:  Value := Data[Row].Col1;
    2:  Value := Data[Row].Col2;
  end;
end;

When column is moved this OnGetValue code works incorrectly (shown columns data on the previous positions). How to fix this? Should I use OnColumnMoved event and remember new columns position manually?


回答1:


Ok, this is my own answer:

We should add helper function to our TRec for reading fields by index:

type
  TRec = record
    Col0,
    Col1,
    Col2: string;
    function GetField(AIndex: Integer): string;
  end;

function TRec.GetField(AIndex: Integer): string;
begin
  case AIndex of
    0:  Result := Col0;
    1:  Result := Col1;
    2:  Result := Col2;
  else
    Result := '';
  end;
end;

Also 2 functions to save and restore TGrid columns using Ini file:

type
  TColumnData = record
    Pos:     UInt8;
    Visible: Boolean;  
    Width:   UInt16;
  end;

procedure LoadColumns(AGrid: TGrid; const ASection, AIdent: string);
var
  I, J, ColsSize: Integer;
  A: TArray<TColumnData>;
  Col: TColumn;
begin
  for I := 0 to AGrid.ColumnCount - 1 do
    AGrid.Columns[I].Tag := I;
  SetLength(A, AGrid.ColumnCount);
  ColsSize := AGrid.ColumnCount*SizeOf(TColumnData);
  if ReadIni(<FileName>, ASection, AIdent, (@A[0])^, ColsSize) = ColsSize then
    for J := 0 to AGrid.ColumnCount - 1 do begin
      for I := 0 to AGrid.ColumnCount - 1 do begin
        Col := AGrid.Columns[I];
        if Col.Tag = A[J].Pos then begin
          Col.Index   := J;
          Col.Visible := A[J].Visible;
          Col.Width   := A[J].Width;
        end;
      end;
    end;
end;

procedure SaveColumns(AGrid: TGrid; const ASection, AIdent: string);
var
  I, ColsSize: Integer;
  A: TArray<TColumnData>;
  Col: TColumn;
begin
  SetLength(A, AGrid.ColumnCount);
  ColsSize := AGrid.ColumnCount*SizeOf(TColumnData);
  for I := 0 to AGrid.ColumnCount - 1 do begin
    Col := AGrid.Columns[I];
    A[I].Pos     := Col.Tag;
    A[I].Visible := Col.Visible;
    A[I].Width   := Round(Col.Width);
  end;
  WriteIni(<FileName>, ASection, AIdent, (@A[0])^, ColsSize);
end;

Now we should call LoadColumns (which is also initialize Tag fields for Columns) from OnFormCreate and SaveColumns from OnFormDestroy. And finally OnGetValue code:

var
  Data: TArray<TRec>;

procedure TFormMain.GridGetValue(Sender: TObject; const Col, Row: Integer; var Value: TValue);
begin
  Value := Data[Row].GetField((Sender as TGrid).Columns[Col].Tag);
end;


来源:https://stackoverflow.com/questions/43418528/fmx-tgrid-ongetvalue-after-column-moving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!