Title: Replace data to a TStringGrid Column By Title
Question: Replace the data in a cell using the Column Title.
Answer:
This is a procedure that calls a function.
The function is used to return the column number of the cell in row O that has StringData as it's title or -1 if it fails:
function ColByTitle(fGridName: TStringGrid; StringData: string): LongInt;
begin
Result:=fGridName.Rows[0].IndexOf(StringData); // thanks Eber
end;
In the Column with Title = Column Title and Row = Arow replace the data with StringData.
If ARow is blank (default) or -1 then replace the last row.
procedure TFHGrid.AddByColumnTitle(fGridName: TStringGrid; ColumnTitle, StringData: string; ARow: LongInt = -1);
begin
with TStringGrid(fGridName) do
begin
if ColByTitle(fGridName, ColumnTitle) = 0 then // find the column title
if ARow = -1 then
Cells[ColByTitle(fGridName, ColumnTitle), RowCount -1] := StringData
// replce the LAST row
else
Cells[ColByTitle(fGridName, ColumnTitle), ARow] := StringData;
// replace the selected row
end;
end;