Solved

Setting DBGrid column widths

Posted on 2000-03-23
9
318 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:jdthedj
9 Comments
 
LVL 7

Expert Comment

by:Motaz
ID: 2648686
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
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2648688
I think you have to use G.Columns[NN].Width
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2648702
Double-click on the grid and 'add all fields'. Now you can change the width and it will be saved.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
ID: 2648737
There are small working sample, it show how to change TDBGrid layout at runtime. You may have open or closed dataset, it doesn't matter;)

--------- //  tablename = biolife.db
procedure TForm1.SpeedButton3Click(Sender: TObject);
var C : TColumn;
begin  
   G.Columns.Clear;

   C:=TColumn.Create(G.Columns);
   C.FieldName:='category';
   C.Alignment:=taRightJustify;
   C.Width:=150;

   C:=TColumn.Create(G.Columns);
   C.FieldName:='common_name';
   C.Width:=150;
   C.Color:=clYellow;
end;
-----------

Cheers,
Igor.
0
 
LVL 2

Expert Comment

by:florisb
ID: 2648816
try  
grid.ColWidths[0] := 100;  grid.ColWidths[1] := 200;

Floris.
0
 
LVL 2

Expert Comment

by:florisb
ID: 2648828
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.
0
 

Expert Comment

by:SuperSy
ID: 2650240
In which procedure do you call your loadRegister procedure?  (OnActivate, OnCreate, etc.)
0
 
LVL 3

Author Comment

by:jdthedj
ID: 2652363
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

0
 
LVL 2

Expert Comment

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

F.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
code issue 8 178
Run video youtube webbrowse 10 73
Delphi: Connect to running MS Outlook 4 156
Convert MS Word document to a PDF file 9 114
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question