Link to home
Start Free TrialLog in
Avatar of APS NZ
APS NZFlag for New Zealand

asked on

Setting DBGrid column widths

Hi

I am trying to restore a DBGrid's layout at runtime from column widths stored in the registry.  If I resize the grid and run the write routine it stores the correct values, but when I try to read them back the grid does not want to take them.

I have singlestepped through my code and noticed that the figure read from the registry seems to be ignored, and a *random* value substituted.

I use the line

G.Columns.Items[NN].Width := ThisColWidth, where G is of type TDBGrid.  I have also tried just G.Columns[NN].Width... but I get the same result.

What am I doing wrong? and how do I get it to work?

Regards

John
Avatar of Motaz
Motaz

1. Try to open the table befor you set the width
2. What is the code of reading and writing to registry, try to post it here.

Motaz
I think you have to use G.Columns[NN].Width
Double-click on the grid and 'add all fields'. Now you can change the width and it will be saved.
ASKER CERTIFIED SOLUTION
Avatar of Igor UL7AAjr
Igor UL7AAjr
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try  
grid.ColWidths[0] := 100;  grid.ColWidths[1] := 200;

Floris.
Reg:

procedure TForm1.SaveRegister;
var
MyReg: TRegistry;
begin
MyReg := TRegistry.Create;
MyReg.RootKey := HKEY_CURRENT_USER;
MyReg.OpenKey('SATLGUI', True);
///save FormSize
MyReg.WriteInteger('ClientWidth',Form1.ClientWidth);
MyReg.WriteInteger('ClientHeight',Form1.ClientHeight);
MyReg.CloseKey;
MyReg.Free;
end;

procedure TForm1.loadRegister;
{*******************************************************************************
temp / test
*******************************************************************************}
/// haal waarden uit registery, gebruik anders dafaults.
var
MyReg: TRegistry;
begin
MyReg := TRegistry.Create;
MyReg.RootKey := HKEY_CURRENT_USER;
///breedte
if MyReg.OpenKey('SATLGUI', False) then
  try
    begin
    ///Eigenlijk zou er een exception handler om elke regel moeten staan, eventueel later
    form1.clientwidth := MyReg.ReadInteger('ClientWidth');
    form1.clientheight := MyReg.ReadInteger('ClientHeight');
//...
et cetera....

MyReg.CloseKey
MyReg.free.
In which procedure do you call your loadRegister procedure?  (OnActivate, OnCreate, etc.)
Avatar of APS NZ

ASKER

Hi Everyone - thanks for the replies.  I have taken time to evaluate each one, and it has still not been easy to get it to work.

After a lot of trial and error I have modified Igor's code snippet, and I have finally got it working.  Here's the routine:

procedure TResultForm.ReadTableIniFile(FileName: String; G: TDBGrid; Key: String);
var MyIni: TRegIniFile; N: Integer; S,S1: String; C: TColumn; TmpStr: TStrings;
begin
     MyIni := TRegIniFile.Create(FileName);
     TmpStr := TStringList.Create;
     MyIni.ReadSectionValues(Key,TmpStr);
     G.Columns.Clear;
     For N := 0 to TmpStr.Count-3 do
     begin
          S := Copy(TmpStr[N],POS('=',TmpStr[N])+1,200);
          S1 := Copy(S,1,POS(',',S)-1);
          Delete(S,1,POS(',',S));
          C := TColumn.Create(G.Columns);
          C.FieldName := S1;
          Delete(S,Length(S)-1,2);
          C.Width := StrToInt(S);
     end;
     try
     S := Copy(TmpStr[TmpStr.Count-2],POS('=',TmpStr[TmpStr.Count-2])+1,200);
     G.Font.Name := S;
     S := Copy(TmpStr[TmpStr.Count-1],POS('=',TmpStr[TmpStr.Count-1])+1,200);
     G.Font.Size := StrToInt(S);
     except
     end;
     Memo1.Free;
     MyIni.Free;
end;

Thanks again for all the replies

John

Still, if you create the columns in designtime and you use grid.ColWidths[x], things could be simpler.

F.