Recent

Author Topic: [SOLVED] How to Save and Load DBGrid's Columns Order and Visibility ..  (Read 13800 times)

m.dj

  • New member
  • *
  • Posts: 7
Hi ,

do we have SaveToFile method for TDBGridColumns ?
Code: [Select]
DBGrid1.Columns.SaveToFile('columns_settings') ;
http://delphi.about.com/od/usedbvcl/a/gridcolumnorder.htm

Thanks
« Last Edit: August 05, 2013, 06:55:04 pm by m.dj »

grandehombre

  • New Member
  • *
  • Posts: 42
Re: How to Save and Load DBGrid's Columns Order and Visibility ..
« Reply #1 on: August 05, 2013, 12:55:37 pm »
I use the following code to take the current grid's column list (order, title and width) and save it to a file (and, further down, load a grid from a file)

Code: [Select]
procedure TSample.saveGridLayout(fileName: string);
var
  lines: TStringList;
  i: integer;
begin
try
lines := TStringList.Create;
                with DBGrid1 do begin
        for i := 0 to DBGrid1.Columns.count-1 do begin
    lines.Add(  IntToStr(DBGrid1.Columns[i].Index)
            + '~ ' + DBGrid1.Columns[i].DisplayName
            + '~ ' + DBGrid1.Columns[i].Title.Caption
            + '~ ' + IntToStr(DBGrid1.Columns[i].Width)
            );
        end;
        end;

lines.SaveToFile(fileName);
finally
        lines.free;
end;
end;

procedure TSample.loadGridLayout(fileName: string);
var
lines: TStringList;
columnInfo: TStringList;
lineCtr: integer;
colIdx: integer;
    cnt: integer;
begin
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:=DBGrid1.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 DBGrid1.Columns[colIdx].FieldName = columnInfo[1] then begin
DBGrid1.Columns[colIdx].Width := StrToInt(columnInfo[3]);
DBGrid1.Columns[colIdx].Title.Caption := columnInfo[2];

               // do the index assignment last!
// ignore the index specified in the file. use its line
DBGrid1.Columns[colIdx].Index := lineCtr; //StrToInt(columnInfo[0]); order instead
end; // if
end;
end;
end;
finally
lines.free;
if assigned(columnInfo) then begin
        columnInfo.free;
end;
end;
end;

// code for the stringExplode method...
Procedure StringExplode(s: string; Delimiter: string; Var res: TStringList);
Begin
res.Clear;
res.Text := StringReplace(s, Delimiter, #13#10, [rfIgnoreCase, rfReplaceAll]);
End;



Note

Please note that the above is a quick extract from my code, that has been cut down for brevity. Test before deplying.
« Last Edit: August 05, 2013, 01:01:39 pm by grandehombre »
Lazarus v1.2RC2  i386-win32-win32/win64,  FPC v2.6.2, svn43696 on Win7-64 and win8.1

m.dj

  • New member
  • *
  • Posts: 7
Re: How to Save and Load DBGrid's Columns Order and Visibility ..
« Reply #2 on: August 05, 2013, 06:54:15 pm »
Thank you for your help

I made a simple modification ( so i can save/load any dbgrid columns i want  :))

Code: [Select]
// code for the stringExplode method...
Procedure StringExplode(s: string; Delimiter: string; Var res: TStringList);
Begin
res.Clear;
res.Text := StringReplace(s, Delimiter, #13#10, [rfIgnoreCase, rfReplaceAll]);
End;   

procedure saveGridLayout(Mydbgrid: TDBGrid; fileName: string);
var
  lines: TStringList;
  i: integer;
begin
try
lines := TStringList.Create;
                with Mydbgrid do
                begin
        for i := 0 to Mydbgrid.Columns.count-1 do
                        begin
    lines.Add(  IntToStr(Mydbgrid.Columns[i].Index)
            + '~ ' + Mydbgrid.Columns[i].DisplayName
            + '~ ' + Mydbgrid.Columns[i].Title.Caption
            + '~ ' + IntToStr(Mydbgrid.Columns[i].Width)
            );
        end;
        end;

lines.SaveToFile(fileName);
finally
        lines.free;
end;
end; 


procedure loadGridLayout(Mydbgrid: TDBGrid; fileName: string);
var
lines: TStringList;
columnInfo: TStringList;
lineCtr: integer;
    colIdx: integer;
        cnt: integer;
begin
 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[1] then
                                begin
Mydbgrid.Columns[colIdx].Width := StrToInt(columnInfo[3]);
Mydbgrid.Columns[colIdx].Title.Caption := columnInfo[2];

               // do the index assignment last!
// ignore the index specified in the file. use its line
Mydbgrid.Columns[colIdx].Index := lineCtr; //StrToInt(columnInfo[0]); order instead
end; // if
end;
end;
end;
 finally
lines.free;
if assigned(columnInfo) then
        columnInfo.free;
 end;
end;

 

TinyPortal © 2005-2018