Mega Code Archive

 
Categories / Delphi / Files
 

Initialize StringGrid from INI file

Title: Initialize StringGrid from INI file Question: 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