Jump to content


Photo

Compact Save/Restore of GridView Layout.


  • Please log in to reply
No replies to this topic

#1 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 18 March 2005 - 10:45 PM

Hi,

Here is a set of routines to save/restore the layout of a TGridView in a more compact way than TGridView.SavetoIni. These routines also allow for more than one TGridView Layout to be stored in a single ini file and keeps the Layout of a GridView in a single IniFile section.

To save the layout call:

CODE
 SaveGridLayout(appinifile, GridView3, 'GridView3.');


To Restore the Layout:

CODE
 RestoreGridLayout(appinifile, GridView3, 'GridView3.');


Using the following routines:

CODE
procedure TForm1.SaveGridLayout(const FileName: string; const GridView: TGridView; const Prefix: string = '');

var

 IniFile           : TIniFile;

 Section           : string;

 Layout            : TStringList;

 i                 : Integer;

begin

 Section := Format('%sLayout', [Prefix]);



 with GridView do

   begin

     IniFile := TIniFile.Create(FileName);

     IniFile.WriteInteger(section, 'ColumnsCount', Columns.Count);

     Layout := TStringList.Create;

     for i := 0 to Columns.Count - 1 do

       begin

         Layout.Clear;

         Layout.Add(IntToStr(Ord(Columns[i].SortKind)));

         Layout.Add(IntToStr(Ord(Columns[i].Visible)));

         Layout.Add(IntToStr(Ord(Columns[i].Sorted)));

         Layout.Add(IntToStr(Ord(Columns[i].Position)));

         Layout.Add(IntToStr(Ord(Columns[i].Width)));

         Layout.Delimiter := ListSeparator;

         IniFile.WriteString(section, Format('Column%d', [i]), Layout.DelimitedText);

       end;

     IniFile.UpdateFile;



     Layout.Free;

     IniFile.Free;

   end;

end;


CODE
procedure TForm1.RestoreGridLayout(const FileName: string; var GridView: TGridView; const Prefix: string = '');

var

 IniFile           : TIniFile;

 Layout            : TStringList;

 Section           : string;

 Count,

   i               : Integer;

begin

 Section := Format('%sLayout', [Prefix]);



 with GridView do

   begin

     IniFile := TIniFile.Create(FileName);

     Layout := TStringList.Create;

     Count := IniFile.ReadInteger(section, 'ColumnsCount', Columns.Count);

     for i := 0 to Count - 1 do

       begin

         Layout.Delimiter := ListSeparator;

         Layout.DelimitedText := IniFile.ReadString(section, Format('Column%d', [i]), '');

         if (Layout.Count = 5) then

           begin

             Columns[i].SortKind := TSortKind(StrToInt(Layout[0]));

             Columns[i].Visible := Boolean(StrToInt(Layout[1]));

             Columns[i].Sorted := Boolean(StrToInt(Layout[2]));

             Columns[i].Position := StrToInt(Layout[3]);

             Columns[i].Width := StrToInt(Layout[4]);

           end;

       end;

     Layout.Free;

     IniFile.Free;

   end;

end;


Have fun coding.
G.W. van der Vegt




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users