Mirror

Initialize StringGrid from INI file (Views: 100)


Problem/Question/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

<< Back to main page