After using MoveColumn for several columns and then doing some MergeCells, some rows can extended beyond the right edge of the last column of the grid. It appears that MoveColumn does a virtual move and not an actual data move like SwapColumns. So, MergeCells which uses the display to real column technique in DoGetCellMergeInfo appears to get confused.
The code below can be used to use either MoveColumn or SwapColumns to show what is actually happening. If I remove the DisplyToRealColumn usage in DoGetCellMergeInfo and just use the real column number, things work correctly. But don't know if that would break other things.
Or should I be moving columns in another way or making some other call that resets things after the moves?
procedure TForm1.FormShow(Sender: TObject);
var
UseSwapRows: boolean;
procedure AddRow(dataStr: string);
var
aStringList: TStringList;
c, r: integer;
begin
aStringList := TStringList.Create;
aStringList.CommaText := dataStr;
TMSFMXGrid1.RowCount := TMSFMXGrid1.RowCount + 1;
r := TMSFMXGrid1.RowCount - 1;
for c := 0 to aStringList.Count - 1 do
begin
TMSFMXGrid1.Cells[c, r] := aStringList[c];
TMSFMXGrid1.HorzAlignments[c,r] := TTextAlign.Center;
end;
aStringList.Free;
end;
procedure MoveColumnViaSwaps(fromCol, toCol: integer);
var
colI: integer;
begin
TMSFMXGrid1.BeginUpdate;
if abs(fromCol - toCol) = 1 then
TMSFMXGrid1.SwapColumns(fromCol, toCol)
else
for colI := fromCol downto (toCol + 1) do
TMSFMXGrid1.SwapColumns(colI, colI - 1);
TMSFMXGrid1.EndUpdate;
end;
begin
{ Setup the grid }
TMSFMXGrid1.RowCount := 0;
TMSFMXGrid1.ColumnCount := 7;
AddRow(',Graph1,Graph2,Graph1,Graph2,Graph1,Graph2');
AddRow(',Both,Both,Male,Male,Female,Female');
AddRow(',ZZ,ZZ,ZZ,ZZ,ZZ,ZZ');
AddRow('D,1.03,0.62,1.42,0.99,0.64,0.24');
AddRow('I,4.23,1.37,3.30,0.85,5.19,1.91');
AddRow('S,0.70,1.37,0.76,1.42,0.64,1.32');
AddRow('C,0.89,0.22,0.94,0.38,0.83,0.05');
{ Set which Move Column method to use }
UseSwapRows := False;
if UseSwapRows then
begin
MoveColumnViaSwaps(3,2);
MoveColumnViaSwaps(5,3);
end
else
begin
TMSFMXGrid1.MoveColumn(3,2);
TMSFMXGrid1.MoveColumn(5,3);
end;
{ The first row will not merge correctly if UseSwapRows is false }
TMSFMXGrid1.MergeCells(1,0,3,1);
TMSFMXGrid1.MergeCells(4,0,3,1);
TMSFMXGrid1.MergeCells(1,2,6,1);
end;
For efficiency, MoveColumns only internally changes the column display list index and not the column data while MergeCells() affects the column data as-is.
We'll investigate if we can find a solution for this, a workaround for now is indeed to work via swap (as this moves the actual column data)