简体   繁体   中英

FMX.TGrid how to allow user to move columns without messing up the data

I made a very simple testproject in Delphi 10.2 using FMX. The setup is simple :

  • TGrid that is binded to a TClientDataSet (done in the designer).
  • button that allows the user to open an XML file

This all works fine and the TGrid is populated with all records from the XML File. The XML file is created by another TClientDataSet from an older project.

Now for the problem.
When I move a column to another position all the data is messed up. I do this by just holding down the mouse on a column and than drag it a few columns to the right.
At first it looks fine, but when you start scrolling vertical, it seems that the data is not in the correct columns anymore.
I have the feeling that it only corrects the data in the visual part of the grid, and as soon as you start scrolling the data is not in the correct columns anymore.

Is this a known bug or is there something wrong with my project.
As I said before, there is absolutely no code in this project all is done in the designer. (except for the clientdataset1.LoadFromFile offcourse)

You can try populate your data manually ( Grid: TGrid; CDS: TClientDataSet ):

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  Col: TColumn;
begin
  CDS.Active := True;
  for I := 0 to CDS.FieldDefs.Count - 1 do begin
    Col := TColumn.Create(Grid);
    Grid.AddObject(Col);
    Col.Header := CDS.FieldDefs[I].Name;
    Col.Tag := I;
  end;
  Grid.RowCount := CDS.RecordCount;
end;

procedure TForm1.GridGetValue(Sender: TObject; const ACol, ARow: Integer; var Value: TValue);
begin
  CDS.First;
  CDS.MoveBy(ARow);
  Value := CDS.Fields[ACol].Text;
end;

And after this you can use my solution for columns: stackoverflow.com/q/43418528/2292722

This fixed it for me. I just move the fields that where moved in the grid also in the ClientDataSet and thus far it seems to work.

procedure TForm1.Grid1ColumnMoved(Column: TColumn; FromIndex, ToIndex: Integer);
var
  FieldFrom : string;
  FieldTo   : string;
begin
  FieldFrom := Grid1.ColumnByIndex(FromIndex).Header;
  FieldTo   := Grid1.ColumnByIndex(ToIndex).Header;

  ClientDataSet1.FieldByName(FieldFrom).Index := FromIndex;
  ClientDataSet1.FieldByName(FieldTo).Index   := ToIndex;
end;

But I just wish there was a better way of knowing from the TColumn which fieldname is involved. Seems like the most significant information is missing from this class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM