TAdvStringGrid
Example 68 : Persisting column states

This is achieved through: grid.ColumnStatesToString: string and grid.StringToColumnStates(value: string); The use of the code is demonstrated in the sample where the column states are persisted in an INI file.
1) Grid initialization This code is responsible for initializing the default view of the grid:
procedure TForm1.FormCreate(Sender: TObject); var i: integer; begin {no fixed columns in grid} advstringgrid1.FixedCols := 0; advstringgrid1.ColCount := 10; advstringgrid1.RowCount := 50; {fill grid with easy to recognize data for this demo} advstringgrid1.LinearFill(false); advstringgrid1.AutoNumberRow(0); {enable column moving & column sizing} advstringgrid1.Options := advstringgrid1.Options + [goColSizing, goColMoving]; {add buttons in the column header cells that will allow column hiding} for i := 0 to AdvStringGrid1.ColCount - 1 do advstringgrid1.AddButton(i,0,16,16,'X',haRight,vaTop); {make sure that buttons on a readonly cell are not disabled} advstringgrid1.ControlLook.NoDisabledButtonLook := true; {important call to set the reference column order of the grid} advstringgrid1.SetColumnOrder; end;
Most important here is the call to grid.SetColumnOrder. After the grid is initialized, with this call the reference column order is set. It is relative to this reference column order that all column moving is tracked. 2) Saving & loading column states After this grid initialisation, saving and loading the full column states to an INI file is as simple as:
procedure TForm1.SaveBtnClick(Sender: TObject); var inifile: TInifile; begin inifile := TIniFile.Create('.\grid.ini'); inifile.WriteString('GRID','SETTINGS',advstringgrid1.ColumnStatesToString); inifile.Free; end; procedure TForm1.LoadBtnClick(Sender: TObject); var inifile: TInifile; s: string; begin inifile := TIniFile.Create('.\grid.ini'); s := inifile.ReadString('GRID','SETTINGS',''); inifile.Free; if s <> '' then AdvStringGrid1.StringToColumnStates(s); end;
Given that persisting user customized grid views is as simple as this, there is no more excuse to not offer this capability to your users!
Delphi project & source files for downloading included in the main demos distribution for Delphi.
×