VCL Delphi

Title: Add Data to a TStringGrid
Question: How do I add text to a TStringGrid using the Column and Row Title?
Answer:
This procedure adds data to a TStringGrid using the Column Title and Row Title as the cross-referenced cell to put the data (Column Title is Row 0; Row title is Column 0)
If there is a blank column or row it will use that column or row and replace the title.
If there is no column or row with the title it will create a new column or row.
Once it has found the cross-referenced cell it will replace or add the data depending on TCellAction and TDataType
uses Grids;
type
TCellAction = (caReplace, caIfGreater, caIfLess, caAdd);
{
caReplace := don't care what is in the cell replace it
caIfGreater := if Stringdata is greater than cell string - replace it
caIfLess := if Stringdata is Less than cell string - replace it
caAdd := Add Stringdata and cell string *** depends on TDataType
}
TDataType = (dtString, dtNumeric);
{
if TCellAction = caAdd and
dtString := treat as strings and concatenate the strings (or digits)
cell string := '1'; stringdata := '4'; result := '14'
dtNumeric := treat as reals and add the values
cell string := '1.1'; stringdata := '4.1'; result := '5.2'
}
Procedure AddByColumnRow(fGridName: TStringGrid; ColumnTitle, RowTitle, StringData: String; DataType: TDataType = dtString; CellAction: TCellAction = caReplace);
var
S, nCol, nRow : integer;
Function NotRealZero(fInputString: String): Real;
{if the cell is blank treat as 0}
begin
if fInputString = '' then
result := 0
else
result := strtofloat(fInputString);
end;
begin
nRow := -1;
nCol := -1;
with TStringGrid(fGridName) do
begin
for S := 0 to ColCount -1 do
// see if the title exists in one of the columns
if (Cells[S, 0] = ColumnTitle) or (Cells[S, 0] = '') then
nCol := S; // if the column title exists OR there is a blank
if (nCol begin
if nCol begin
ColCount := ColCount + 1;
Cells[ColCount -1, 0] := ColumnTitle;
nCol := ColCount -1;
end
else
Cells[ncol, 0] := ColumnTitle; // rename '' to column Title
end;
for S := 0 to RowCount -1 do
// see if the title exists in one of the rows
if (Cells[0, S] = RowTitle) or (Cells[0, S] = '') then
nRow := S; // if the row title exists OR there is a blank
if (nRow begin
if nRow begin
RowCount := RowCount + 1;
Cells[0, RowCount -1] := RowTitle;
nRow := RowCount -1;
end
else
Cells[0, nRow] := RowTitle; // rename '' to row Title
end;
case CellAction of
caReplace : Cells[nCol, nRow] := StringData;
caIfGreater :
begin
if DataType = dtString then
if Cells[nCol, nRow] Cells[nCol, nRow] := StringData
else
if (notrealzero(Cells[nCol, nRow]) Cells[nCol, nRow] := StringData;
end;
caIfLess :
begin
if DataType = dtString then
if (Cells[nCol, nRow] StringData) or (Cells[nCol, nRow] = '') then
Cells[nCol, nRow] := StringData
else
if (Cells[nCol, nRow] = '') or (notrealzero(Cells[nCol, nRow]) notrealzero(StringData)) then
Cells[nCol, nRow] := StringData;
end;
else
begin
if DataType = dtString then
Cells[nCol, nRow] := Cells[nCol, nRow] + StringData
else
Cells[nCol, nRow] := floattostr(NotRealZero(Cells[nCol, nRow]) + NotRealZero(StringData));
end; //case
end; // with
end;