TAdvStringGrid
Example 68 : Persisting column states
Often it is desirable to allow that the user can resize columns, move columns, hide columns of a grid. After a user customizes the view of the grid this way, it would be convenient to persist this setting and when the application is restarted, these customizations of the user are restored exactly as the user left the application. TAdvStringGrid offers saving column sizes with grid.SaveColSizes and offers saving positions of columns with grid.SaveColPositions. With these 2 methods, it is possible to save column widths & column positions either to registry or to an INI file. With a new and much more convenient method, it is now possible to persist the column widths, column positions and column's visible state with a single call and as the state of the columns is saved to a string, this string can be stored in the registry, an INI file or even a database.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.
×