Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (5)


Initialize StringGrid from INI fileFormat this article printer-friendly!Bookmark function is only available for registered users!
an easy way to initialize any StringGrid
Product:
Delphi all versions
Category:
GUI
Skill Level:
Scoring:
Last Update:
01/25/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet stringgrid tstringgrid grid init ini inifile
Times Scored:
4
Visits:
5320
Uploader: Eber Irigoyen
Company: BTXSys
Reference: N/A
 
Question/Problem/Abstract:
when we use StringGrid many times have a hard time initializing the columns, and the widths, etc...
Answer:



here's my approach to this issue, as I use a lot of stringgrids to create reports or show information... so I ended up creating this function to initialize its headers and stuff

Procedure InitGridFromINI(StrGrid:TStringGrid; Const Section:String; GridINI:TIniFile);
Var X:Integer;
Begin
  With StrGrid Do
  Begin
    RowCount:=GridINI.ReadInteger(Section, 'RowCount', 0)+1;
    ColCount:=GridINI.ReadInteger(Section, 'ColCount', 0);
    For X:=1 To RowCount-1 Do
      Cells[0, X]:=GridINI.ReadString(Section, 'TitleY'+IntToStr(X), '');
    For X:=1 To ColCount Do
    Begin
      Cells[X-1, 0]:=GridINI.ReadString(Section, 'TitleX'+IntToStr(X), '');
      ColWidths[X-1]:=GridINI.ReadInteger(Section, 'ColW'+IntToStr(X), DefaultColWidth)
    End
  End
End;

pretty simple, has 3 parameters,
the first one of course the StringGrid to be initialized
the section wich would be the section in your INIFile where the parameters are,
and the INI file itself to extract the parameters from

as you may figure out your INIfile would look like this:
file: myprogram.ini
...
[grid]
RowCount=20
ColCount=4
TitleX1=FileName
TitleX2=Error description
TitleX3=Line
TitleX4=Tracking Number
ColW1=90
ColW2=250
ColW3=50
ColW4=200
...

//...pretty self explanative... I hope

InitGridFromINI(StringGrid, 'grid', MyIniFile);

that's it

keep up coding
EberSys





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Insufficient 2
    Hans Pieters (Jan 26 2002 1:40PM)

If you need to create the stringgrids during runtime then you should - in case of calamity - provide the user at least with an elegant way to correct the situation. To show how to provide such a framework I changed your procedure, making it far more robust. Like your example, it doesn't have all the required functionality yet!  

Respond

insufficient 1
    Hans Pieters (Jan 26 2002 1:37PM)

An application should be robust. One of the obligatoiry parts of that is to make your app's self-repairing or at least repairable by the user. A consequence of this is that you should never ever place critical information outside your application - inifile or registry  - without providing a default value within your application.  

Users will find their creative way to delete or corrupt your inifile. And if the user does not do the job, the operating system will one day be so kind to take care of destroying your external parameters.

I don't understand your '....have a hard time initialising the columns, and the widths, etc...' because whatever way you go, you have to calculate and write the titles and the width of the columns.
The simplest and safest way is to place the setup of each stringgrid in separate function. Something like:

function setup_dispgrid1: boolean;
begin
result := true;
try
    with mainform.dispgrid do
    begin
       rowcount := 20 ;
       colcount := 14 ;
      
       cols[0].Clear;
       cols[0].Add('Filename');
       ColWidths[0] := 90 ;
       ...
    end ;
  except
    result := false ;
    messagebox('Dispgrid not ready. Call .....');
  end;
end;

See my second comment...
Hans Pieters
Respond

Insufficient 2
Hans Pieters (Jan 26 2002 1:41PM)

If you need to create the stringgrids during runtime then you should - in case of calamity - provide the user at least with an elegant way to correct the situation. To show how to provide such a framework I changed your procedure, making it far more robust. Like your example, it doesn't have all the required functionality yet!  

Respond

Insufficient 3
Hans Pieters (Jan 26 2002 1:42PM)

function setup_dispgrid2(
  StrGrid: TStringGrid;
  const Section: string;
  GridIni: TIniFile): boolean;
var
  itmp: integer;
  ctmp: string ;
begin
  result := true;
  try
    with StrGrid do
    begin

      rowcount := GridIni.ReadInteger(Section, 'RowCount', 0);
      if rowcount = 0 then
      begin
        rowcount := 1;
        GridIni.writeinteger(Section, 'RowCount', 0);
        result := false;
      end;

      ColCount := GridIni.ReadInteger(Section, 'ColCount', 0);
      if colcount = 0 then
      begin
        colcount := 1;
        GridIni.writeinteger(Section, 'ColCount', 0);
        result := false;
      end;

      for itmp := 0 to colcount - 1 do
      begin
  
        ctmp := IntToStr(itmp);
        cols[itmp].Clear;

        cols[itmp].Add(GridIni.ReadString(Section, 'ColTitleX' + ctmp, '?'));
        if cells[0, itmp] = '?' then
        begin
          GridIni.writeString(Section, 'ColTitleX' + ctmp, '?');
          result := false;
        end;

        colwidths[itmp] := GridIni.ReadInteger(Section, 'ColWidth' + ctmp, 0);
        if colwidths[itmp] = 0 then
        begin
          colwidths[itmp] := 1;
          GridIni.writeString(Section, 'ColWidth' + ctmp, '0');
          result := false;
        end;

      end;

    end;
    if not result then
      messagebox(
        'Grid was not completely initialized' +
        #13'Open the application Ini-file and ' +
        #13'look for section[' + section + '].' +
        #13'Enter a correct value for the items' +
        #13'with an =? or an =0'+
        #13'Problems? Call Eber Irigoyen');

  except
    result := false;
    messagebox('Grid not ready. Call ...');
  end;
end;

Respond

Insufficient 4
Hans Pieters (Jan 26 2002 1:43PM)

The main difference with your approach is, that when the section is nonexistent the section and keys are created and sets its initial value to a recognisable value. You can help even your most ignorant user by telephone to enter the required parameters. In that case you should start with adaptint the colcount and the rowcount. Restart the application and the procedure with the stringrid. After this the framework will be completed with keys for all columns.

More coding, please.... :-)
Hans Pieters
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
A. B. Talal
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)