Solved

Setting DBGrid column widths

Posted on 2000-03-23
9
310 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

13 Experts available now in Live!

Get 1:1 Help Now