Solved

Setting DBGrid column widths

Posted on 2000-03-23
9
317 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection between libmysql.dll and MySQL Versions 7 112
drawing animated level bar based on numbers 3 104
Dev express lookupcombo 3 35
shape, triangle, dbctrlgrid 3 17
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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