TGridRow =
record
constructor Create(AGrid: TStringGrid; ARowNum: Integer);
private
FGrid: TStringGrid;
FRowNum: Integer;
function GetColumn(
const AName:
string):
string;
procedure SetColumn(
const AName, AValue:
string);
public
property Column[
const AName:
string]:
string read GetColumn
write SetColumn;
default;
end;
TColumnHelper =
class helper
for TStringGrid
private
procedure CheckRowNum(
const ARowNum: Integer);
function GetGridRow(
const ARowNum: Integer): TGridRow;
protected
function GetColumnIndex(
const AName:
string): Integer;
public
property GridRow[
const ARowNum: Integer]: TGridRow
read GetGridRow;
end;
implementation
{ TColumnHelper }
procedure TColumnHelper.CheckRowNum(
const ARowNum: Integer);
begin
if ARowNum < 1
then
raise Exception.CreateFmt('
TColumnHelper RowNum(%d) invalid < 1', [ARowNum]);
if ARowNum >= RowCount
then
raise Exception.CreateFmt('
TColumnHelper RowNum(%d) invalid >= RowCount(%d)', [ARowNum, RowCount]);
end;
function TColumnHelper.GetColumnIndex(
const AName:
string): Integer;
var
i: Integer;
begin
for i := 0
to ColCount - 1
do
begin
if SameText(AName, Cells[i, 0])
then
Exit(i);
end;
raise Exception.CreateFmt('
TColumnHelper Column[''
%s''
] invalid', [AName]);
end;
function TColumnHelper.GetGridRow(
const ARowNum: Integer): TGridRow;
begin
CheckRowNum(ARowNum);
Result := TGridRow.Create(Self, ARowNum);
end;
{ TGridRow }
constructor TGridRow.Create(AGrid: TStringGrid; ARowNum: Integer);
begin
FGrid := AGrid;
FRowNum := ARowNum;
end;
function TGridRow.GetColumn(
const AName:
string):
string;
begin
Result := FGrid.Cells[FGrid.GetColumnIndex(AName), FRowNum];
end;
procedure TGridRow.SetColumn(
const AName, AValue:
string);
begin
FGrid.Cells[FGrid.GetColumnIndex(AName), FRowNum] := AValue;
end;