delphifiremonkeytclientdatasettgriddelphi-10.2-tokyo

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 :

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)


Solution

  • 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.