2

Im working in a Tdbadvgrid from TMS. I am adding colums dynamically based on a sql query length. Everything works great, with the exception of the column width saving. Adding the columns:

  //Captions
  for i := 0 to oRow.Count - 1 do
  begin
    grdFieldData.Columns.Insert(1);
    grdFieldData.Cells[i + 1, 0] := TabelList.Captions[i].Caption;
  end;

  //Data
  for r := 0 to TabelList.Count - 1 do //rows
  begin
    for c := 0 to oRow.Count - 1 do //cols
    begin
      grdFieldData.Cells[c+1, r+1] := TabelList.Rows[r].Fields[c].Value;
    end;
    if r <> TabelList.Count - 1 then
      grdFieldData.RowCount := grdFieldData.RowCount + 1;
  end;

Now the save function is built into the TMS TDBAdvGrid, and looks like this: ColumnSize option

I have tried messing with all the options with no luck.

The table has 1 fixed row (for captions) and one empty row. The row is only there because the number of fixed rows must be smaller then the number of rows. When saving the data to either .ini file or to registry, it saves and loads the first column, but the dynamicly added ones get written in as default value (64) but never saved/loaded when i drag them to adjust size. The .ini file table looks like this:

[Recept]
Col0=20
Col1=97
Col2=64
Col3=64
Col4=64
Col5=64
Col6=64
Col7=64
Col8=64
Col9=64
Col10=64
Col11=64
Col12=64
Col13=64
Col14=64

When loaded in it looks like this: The Grid

Does anyone know what I can do to make the columns save properly so the widths will be saved?

H4rdstyler
  • 71
  • 1
  • 11
  • Have you tried to handle save/load procedure on your own? You should have a look into their code and check when it gets called. Maybe it is not suitable for this situation or you have to manually call save/load functions when needed. – smooty86 Mar 14 '16 at 08:45
  • I have tried managing the save and load myself, yes. It changes nothing sadly. – H4rdstyler Mar 14 '16 at 08:46
  • I suggest posting this in the TMS support forum. They almost always respond within 24 hours and there us a very good chance that they will resolve your problem. – TomT Mar 14 '16 at 10:53
  • Also, when receive the answer from TMS, post it here. Somebody facing the same issue will thank you for that, and maybe buy you a beer :) – RBA Mar 14 '16 at 12:57
  • My superviser isnt sure where he has the login information for the tms website, and has told me to downprioritize it for the moment. But ive built a custom sulotion to load and save col widths to an ini file. I will gladly post that, but should I considering its a patch solution? Allthou working flawlessly :) – H4rdstyler Mar 14 '16 at 13:54

1 Answers1

1

So, after a few hours of sitting with it, I decided that there was no reason to struggle so much with something built in when I could just build something simple myself.

In this instance, it simply saves to a chosen ini file at a chosen directory. The filename and directory are currently hardcoded to each inhreited class. A feature to change them could easily be implemented. Perhaps even a directory selecter to a button or something like it.

The code for loading (called on FormShow):

procedure TfrmReceptEditor.LoadColWidths;
var
  Ini: TIniFile;
  i: Integer;
  path: String;
  filename: String;
begin
  inherited;
  path := 'C:\';
  filename := 'grid.ini';
  Ini := TIniFile.Create(path + filename);
  try
    for i := 0 to grdFieldData.ColCount - 1 do
    begin
      grdFieldData.Columns.Items[i].Width := Ini.ReadInteger('Recept','col'+IntToStr(i),75);
    end;
    grdFieldData.FixedColWidth := 20;
  finally
    Ini.Free;
  end
end;

And the code for saving the data (on FormClose):

procedure TfrmReceptEditor.SaveColWidths;
var
  Ini: TIniFile;
  i: Integer;
  path: String;
  filename: String;
begin
  inherited;
  path := 'C:\';
  filename := 'grid.ini';
  Ini := TIniFile.Create(path + filename);
  try
    for i := 0 to grdFieldData.ColCount - 1 do
    begin
      Ini.WriteInteger('Recept', 'col'+IntToStr(i), grdFieldData.Columns.Items[i].Width);
    end;
  finally
    Ini.Free;
  end;
end;

Some might want to build in features to check if the Ini files exists (aka if the entered path is correct). But it will work smoothly even if the file doesnt exist, it will simply create it.

H4rdstyler
  • 71
  • 1
  • 11