Accessing a grid's column/row by name

Question:

Is it possible to access a grid row or column by name?

Answer:

The following example presents two functions: GetGridColumnByName() and GetGridRowByName() that return the given column or row that contains the specified caption.

 

procedure TForm1.FormCreate(Sender: TObject);

begin

  StringGrid1.Rows[1].Strings[0] := 'This Row';

  StringGrid1.Cols[1].Strings[0] := 'This Column';

end;

 

function GetGridColumnByName(Grid : TStringGrid;

  ColName : string): integer;

var

  i : integer;

begin

  for i := 0 to Grid.ColCount - 1 do

  if Grid.Rows[0].Strings[i] = ColName then begin

  Result := i;

  exit;

  end;

  Result := -1;

end;

 

function GetGridRowByName(Grid : TStringGrid;

  RowName : string): integer;

var

  i : integer;

begin

  for i := 0 to Grid.RowCount - 1 do

  if Grid.Cols[0].Strings[i] = RowName then begin

  Result := i;

  exit;

  end;

  Result := -1;

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

  Column : integer;

  Row : integer;

begin

  Column := GetGridColumnByName(StringGrid1, 'This Column');

  if Column = -1 then

  ShowMessage('Column not found') else

  ShowMessage('Column found at ' + IntToStr(Column));

  Row := GetGridRowByName(StringGrid1, 'This Row');

  if Row = -1 then

  ShowMessage('Row not found') else

  ShowMessage('Row found at ' + IntToStr(Row));

end;