Here is the code above a bit modified and without the bugs (this time for the mobile version):
PROCEDURE TUniMainModule.StringExplode(s : STRING; Delimiter : STRING; VAR res : TStringList);
BEGIN
res.Clear;
res.Text:=StringReplace(s, Delimiter, #13#10, [rfIgnoreCase, rfReplaceAll]);
END;
PROCEDURE TUniMainModule.SaveGridLayout(Mydbgrid : TUnimDBGrid);
VAR filename : STRING;
lines : TStringList;
i : INTEGER;
my_visible : STRING;
BEGIN
Filename:=UniServerModule.StartPath+'GRID'+IntToStr(Mydbgrid.Tag)+'.INI';
TRY
lines:=TStringList.Create;
WITH Mydbgrid DO BEGIN
FOR i:=0 TO Mydbgrid.Columns.count-1 DO BEGIN
IF Mydbgrid.Columns[i].Visible=TRUE THEN my_visible:='T'
ELSE my_visible:='F';
lines.Add(
Mydbgrid.Columns[i].DisplayName+';;'
+IntToStr(Mydbgrid.Columns[i].Width)+';;'
+Mydbgrid.Columns[i].Title.Caption+';;'
+my_visible);
END;
END;
lines.SaveToFile(fileName);
FINALLY
lines.free;
END;
END;
PROCEDURE TUniMainModule.LoadGridLayout(Mydbgrid : TUnimDBGrid);
VAR filename : STRING;
lines : TStringList;
columnInfo : TStringList;
lineCtr : INTEGER;
colIdx : INTEGER;
cnt : INTEGER;
BEGIN
Filename:=UniServerModule.StartPath+'GRID'+IntToStr(Mydbgrid.Tag)+'.INI';
IF NOT TFile.Exists(Filename) THEN Exit;
TRY
lines:=TStringList.Create;
columnInfo:=TStringList.Create;
lines.LoadFromFile(fileName);
FOR lineCtr:=0 TO lines.count-1 DO BEGIN
IF trim(lines[lineCtr])<>'' THEN BEGIN
StringExplode(lines[lineCtr], ';;', columnInfo);
cnt:=Mydbgrid.Columns.count;
// go through all the columns, looking for the one we are currently working on
FOR colIdx:=0 TO cnt-1 DO BEGIN
// once found, set its width and title, then its index (order)
IF Mydbgrid.Columns[colIdx].FieldName=columnInfo[0] THEN BEGIN
Mydbgrid.Columns[colIdx].Width:=StrToInt(columnInfo[1]);
Mydbgrid.Columns[colIdx].Title.Caption:=columnInfo[2];
IF columnInfo[3]='T' THEN Mydbgrid.Columns[colIdx].Visible:=TRUE ELSE Mydbgrid.Columns[colIdx].Visible:=false;
Mydbgrid.Columns[colIdx].Index:=lineCtr;
END;
END;
END;
END;
FINALLY
lines.free;
IF assigned(columnInfo) THEN columnInfo.free;
END;
END;
To make it work, put this code into your MainModule, add "unimDBGrid" and "System.IOUtils" into the uses segment and give every grid in your app a different Tag-property.
Use LoadGridLayout / SaveGridLayout with the corresponding grid. It's recommend to load all grids in the FormShow-event and save them in the FormCloseEvent.
For desktop-version change "TUnimDBGrid" to "TUniDBGrid".