Skip to content

Commit 2563c45

Browse files
committed
Improved demo, added a method to remember and restore the current row selected in the grid, so when sorting the grid we can re-select again the same item (that is now at a different row because rows have been sorted)
1 parent 663ce42 commit 2563c45

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

demos/FireMonkey/TeeGridFeatures/Views/Unit_Custom_Sorting.fmx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ object FormCustomSorting: TFormCustomSorting
1414
DesignerMasterStyle = 0
1515
object TeeGrid1: TTeeGrid
1616
Columns = <>
17-
ReadOnly = False
1817
Align = Client
1918
Size.Width = 640.000000000000000000
2019
Size.Height = 503.000000000000000000

demos/FireMonkey/TeeGridFeatures/Views/Unit_Custom_Sorting.pas

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ procedure TFormCustomSorting.FormCreate(Sender: TObject);
107107

108108
// Set grid data
109109
TeeGrid1.Data:=TVirtualArrayData<TLocation>.Create(Locations);
110+
111+
// Cosmetics
110112
TeeGrid1.Columns.Items[0].Width.Value:= 150;
111113
TeeGrid1.Columns.Items[1].Width.Value:= 150;
112114
end;
@@ -180,18 +182,61 @@ TLocationComparer=class(TComparer<TLocation>)
180182
end;
181183

182184
procedure TFormCustomSorting.SortData(const AColumn:TColumn; const Ascending:Boolean);
185+
186+
// Returns the current selected Location in the grid, if any
187+
function CurrentLocation:TLocation;
188+
var row : Integer;
189+
begin
190+
row:=TeeGrid1.Selected.Row;
191+
192+
if row=-1 then
193+
begin
194+
result.Name:='';
195+
result.Country:='';
196+
end
197+
else
198+
result:=Locations[row];
199+
end;
200+
201+
// Finds the index of ALocation in our array, or -1
202+
function FindLocation(const ALocation:TLocation):Integer;
203+
var t : Integer;
204+
begin
205+
result:=-1;
206+
207+
if ALocation.Name<>'' then
208+
for t:=0 to High(Locations) do
209+
if (Locations[t].Name=ALocation.Name) and
210+
(Locations[t].Country=ALocation.Country) then
211+
begin
212+
result:=t;
213+
break;
214+
end;
215+
end;
216+
183217
var Comparer : TLocationComparer;
218+
Current : TLocation;
184219
begin
220+
// Remember the current selection Location record in the grid, if any
221+
Current:=CurrentLocation;
222+
185223
Comparer:=TLocationComparer.Create;
186224
try
187225
Comparer.Ascending:=Ascending;
188226

227+
// Choose a column
189228
if AColumn.Header.Text='Name' then
190229
Comparer.Field:=TLocationField.Name
191230
else
192231
Comparer.Field:=TLocationField.Country;
193232

233+
// Do the Sort !
194234
TArray.Sort<TLocation>(Locations,Comparer);
235+
236+
// Set back again the current selected Location
237+
if Current.Name<>'' then
238+
TeeGrid1.Selected.Row:=FindLocation(Current);
239+
195240
finally
196241
Comparer.Free;
197242
end;

0 commit comments

Comments
 (0)