0
votes

I have Inherited form and a Ehlib dbgrid on it for selecting-listing records... The form is ready made for a lot of buttons and im using this form with different queries.

Like this...

If Dm.QrTmp.Active then Dm.QrTmp.Active:=False;
Dm.QrTmp.SQL.Clear;
Dm.QrTmp.SQL.Add('  SELECT          ');
Dm.QrTmp.SQL.Add('    ch.cari_RECno AS KayitNo           ');
Dm.QrTmp.SQL.Add('  FROM CARI_HESAPLAR ch   ');
if FrmTmp=nil then FrmTmp:=TFrmTmp.Create(Self);
FrmTmp.StatusBar.Hide;
Dm.QrTmp.Open;
FrmTmp.DbGrid.DataSource:=Dm.DsQrTmp;

This query is cutted down but i have of course use a lot of fields. And Queries changes alot of time in the application.

The problem is column width. Manager wants to set column widths and restore them again. Actually my grid component supports save - restore column properties but as you can see my usage i m not using static columns. also i dont want to use xgrid.columns[0].width percent by percent.

Im using a ini in may app.

I want to add new section on it and named "Gridwidth"...

[Gridname] Colwidths=x,y,z (where they are width values)


I'm now coding this line by line.


My write procedure is like this.

With dbgridx do
  begin
    For i:=0 to columns.count-1 
      begin
         widthstr:=widthstr+Column[i].width+',';  
      end;
  end;

Widthstr will be "15,23,45,67" etc...

But i want to know if this is good solution and if somebody know a better way and has some good code.

1
That doesn't seem an unreasonable solution. Feel free to implement it. - David Heffernan
Maybe when loading the values back put the grid in an non-updating state though till you loaded them to avoid flicker. - Marco van de Voort
I really cant understand that why this post downvoted. I like researching and also im trying to solve it by myslef. Im not asking only "give my ready code". Then what is the problem ? - ikutluay
Why do you want to save it as a single value? This just means you have to write the code to parse it back out again. Save it as individual column widths, and then you can read it back in as separate integer values without parsing at all. - Ken White
Dear Ken White; In the app; more than 100 different grid exists and working column by column by column not an easy solution. But if you proposing With dbgridx do begin For i:=0 to columns.count-1 begin Write_ini_line for Column[i] with column header as inivariablename. end; I think i should try this. Because i was already thinking about reread+parse problem when asking the question... - ikutluay

1 Answers

1
votes

This should do it:

uses
  IniFiles;

const
  SETTINGS_FILE = 'Edijus\Settings.ini';

procedure TForm1.LoadDBGridColumnsWidth(const ADBGrid: TDBGrid);
var
  _MemIniU: TMemIniFile;
  _SettingsPath: string;
  i, j: integer;
  _ParentClass: TWinControl;
begin
  _SettingsPath := GetHomePath + PathDelim + SETTINGS_FILE;
  if (not Assigned(ADBGrid)) or (not Assigned(ADBGrid.DataSource)) or
    (not Assigned(ADBGrid.DataSource.DataSet)) then
    Exit;

  _MemIniU := TMemIniFile.Create(_SettingsPath, TEncoding.UTF8);
  try
    _ParentClass := ADBGrid.Parent;
    while not(_ParentClass is TForm) do
      _ParentClass := _ParentClass.Parent;
    for i := 0 to Pred(ADBGrid.DataSource.DataSet.Fields.Count) do
      for j := 0 to Pred(ADBGrid.Columns.Count) do
      begin
        if (ADBGrid.DataSource.DataSet.Fields[i].FieldName = ADBGrid.Columns[j]
          .FieldName) then
          ADBGrid.Columns[j].Width :=
            _MemIniU.ReadInteger(_ParentClass.Name + '_' + ADBGrid.Name,
            ADBGrid.Columns[j].FieldName, 64);
      end;
  finally
    FreeAndNil(_MemIniU);
  end;
end;

procedure TForm1.SaveDBGridColumnsWidth(const ADBGrid: TDBGrid);
var
  _MemIniU: TMemIniFile;
  _SettingsPath: string;
  i: integer;
  _ParentClass: TWinControl;
begin
  _SettingsPath := GetHomePath + PathDelim + SETTINGS_FILE;
  if (not Assigned(ADBGrid)) or
    (not ForceDirectories(ExtractFilePath(_SettingsPath))) then
    Exit;

  _MemIniU := TMemIniFile.Create(_SettingsPath, TEncoding.UTF8);
  try
    _ParentClass := ADBGrid.Parent;
    while not(_ParentClass is TForm) do
      _ParentClass := _ParentClass.Parent;
    for i := 0 to Pred(ADBGrid.Columns.Count) do
      if (ADBGrid.Columns[i].FieldName <> '') then
        _MemIniU.WriteInteger(_ParentClass.Name + '_' + ADBGrid.Name,
          ADBGrid.Columns[i].FieldName, ADBGrid.Columns[i].Width);

    _MemIniU.UpdateFile;
  finally
    FreeAndNil(_MemIniU);
  end;
end;