我对Firemonkey Stringgrid进行排序的基本尝试,因为找不到任何其他示例。我欢迎任何有关最佳实现目标的优化或其他建议。我的代码基本上会选择要排序的列,并为其他列完成排序顺序:
procedure TfrmMain.StringGridSort(StrGrid: TStringGrid; SortColumn: Integer);
var
col, row, rowx: Integer;
MySortCol, MyListCols: TStringList;
begin
MySortCol := TStringList.Create;
MyListCols := TStringList.Create;
try
MySortCol.Sort;
MyListCols.Sorted := False;
StrGrid.BeginUpdate;
try
for row := 0 to StrGrid.RowCount - 1 do
MySortCol.AddObject(StrGrid.Cells[SortColumn, row], TObject(row));
MySortCol.Sorted := True;
for row := 0 to StrGrid.RowCount - 1 do
StrGrid.Cells[SortColumn, row] := MySortCol[row];
for col := 0 to StrGrid.ColumnCount - 1 do
if col <> SortColumn then
begin
MyListCols.Clear;
for row := 0 to StrGrid.RowCount - 1 do
MyListCols.Add(StrGrid.Cells[col, row]);
for rowx := 0 to StrGrid.RowCount - 1 do
StrGrid.Cells[col, rowx] :=
MyListCols[Integer(MySortCol.Objects[rowx])];
end;
finally
StrGrid.EndUpdate;
end;
finally
MySortCol.Free;
MyListCols.Free;
end;
end;
先谢谢保罗