APS NZ
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try
grid.ColWidths[0] := 100; grid.ColWidths[1] := 200;
Floris.
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('Client Width',For m1.ClientW idth);
MyReg.WriteInteger('Client Height',Fo rm1.Client Height);
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('ClientW idth');
form1.clientheight := MyReg.ReadInteger('ClientH eight');
//...
et cetera....
MyReg.CloseKey
MyReg.free.
procedure TForm1.SaveRegister;
var
MyReg: TRegistry;
begin
MyReg := TRegistry.Create;
MyReg.RootKey := HKEY_CURRENT_USER;
MyReg.OpenKey('SATLGUI', True);
///save FormSize
MyReg.WriteInteger('Client
MyReg.WriteInteger('Client
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('ClientW
form1.clientheight := MyReg.ReadInteger('ClientH
//...
et cetera....
MyReg.CloseKey
MyReg.free.
In which procedure do you call your loadRegister procedure? (OnActivate, OnCreate, etc.)
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.ReadTableIniFi le(FileNam e: String; G: TDBGrid; Key: String);
var MyIni: TRegIniFile; N: Integer; S,S1: String; C: TColumn; TmpStr: TStrings;
begin
MyIni := TRegIniFile.Create(FileNam e);
TmpStr := TStringList.Create;
MyIni.ReadSectionValues(Ke y,TmpStr);
G.Columns.Clear;
For N := 0 to TmpStr.Count-3 do
begin
S := Copy(TmpStr[N],POS('=',Tmp Str[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[Tmp Str.Count- 2])+1,200) ;
G.Font.Name := S;
S := Copy(TmpStr[TmpStr.Count-1 ],POS('=', TmpStr[Tmp Str.Count- 1])+1,200) ;
G.Font.Size := StrToInt(S);
except
end;
Memo1.Free;
MyIni.Free;
end;
Thanks again for all the replies
John
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.ReadTableIniFi
var MyIni: TRegIniFile; N: Integer; S,S1: String; C: TColumn; TmpStr: TStrings;
begin
MyIni := TRegIniFile.Create(FileNam
TmpStr := TStringList.Create;
MyIni.ReadSectionValues(Ke
G.Columns.Clear;
For N := 0 to TmpStr.Count-3 do
begin
S := Copy(TmpStr[N],POS('=',Tmp
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
G.Font.Name := S;
S := Copy(TmpStr[TmpStr.Count-1
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.
F.
2. What is the code of reading and writing to registry, try to post it here.
Motaz