Solved

Setting DBGrid column widths

Posted on 2000-03-23
9
315 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now