将所有.ini值读入stringgrid

时间:2016-01-14 10:00:19

标签: delphi delphi-xe delphi-xe7

我使用的是Delphi XE7。有没有办法将.ini文件中的所有值加载到不同coloumns的stringgrid中?

我的.ini文件看起来像

[1038]
AValue = a1 
BValue = b1
CValue = c1
DValue = d1

[1031]
AValue = a2 
BValue = b2
CValue = c2
DValue = d2

我使用此程序填充网格:

procedure TForm1.ReadIntoGrid(const aIniFileName, aSection: string;
const aGrid: TStringGrid);
var
  Ini: TIniFile;
  SL: TStringList;
  i: Integer;
begin
  SL := TStringList.Create;
  try
    Ini := TIniFile.Create(aIniFileName);
    try
      aGrid.ColCount := 2;
      Ini.ReadSectionValues(aSection, SL);
      aGrid.RowCount := SL.Count;

      for i := 0 to SL.Count - 1 do
      begin
        aGrid.Cells[0,i] := SL.Names[i];
        aGrid.Cells[1,i] := SL.ValueFromIndex[i];
      end;
    finally
      Ini.Free;
    end;
  finally
    SL.Free;
  end;
end;

工作正常,我明白了:

enter image description here

我的问题是...... 如何将所有截面值(1038和1031)读入1038值旁边的网格?值将一直固定。

enter image description here

1 个答案:

答案 0 :(得分:4)

给你一些想法:

首先,我认为你应该在你的程序中添加一个参数:

procedure TForm1.ReadIntoGrid(const aIniFileName, aSection: string;
const aGrid: TStringGrid; const aColumn: Integer = 1);

其次,重写方法的这一部分:

  for i := 0 to SL.Count - 1 do
  begin
    aGrid.Cells[0,i] := SL.Names[i];
    aGrid.Cells[1,i] := SL.ValueFromIndex[i];
  end;

替换为

  for i := 0 to SL.Count - 1 do
  begin
    aGrid.Cells[0,i] := SL.Names[i];
    aGrid.Cells[aColumn,i] := SL.ValueFromIndex[i];
  end;

ps:显然你不必将值重写到第一列。

所以现在假设您正在调用这样的方法:

ReadIntoGrid('MyIniFile.ini','1038', MyGrid, 1);
ReadIntoGrid('MyIniFile.ini','1031', MyGrid, 2);
相关问题