Question

I am selecting a certain cell in a TAdvStringGrid:

const
  MyCol=4;
  MyRow=1;
  HiddenCol=2;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AdvStringGrid1.ColCount:=5;
  AdvStringGrid1.RowCount:=10;
end;

procedure TForm1.BtnHideClick(Sender: TObject);
begin
  AdvStringGrid1.HideColumn(2);
end;

procedure TForm1.BtnSelectCellClick(Sender: TObject);
begin
  AdvStringGrid1.SelectCells(MyCol,MyRow,MyCol,MyRow);
end;

However, my problem is that after the column is hidden, the cell I need to be selected won't be selected, since the program is seeing that the ColCount is now 4 and the cell at col 5 no more exists. How can I still select the cell regardless the hidden column?

By selecting I mean to focus the cell, and show the user what cell is been selected, not just read its string value.

Was it helpful?

Solution

According to the TAdvStringGuide v6.1 Developers Guide on page 57, you can use grid.AllCells(ACol, ARow). The description says: "Access the grid cell as string irrespective of hidden columns or rows. grid.AllCells returns the cell as displayed, ie. after possible processing of the real cell text by the event OnGetDisplText".

To show the selected cell, you could make use of some additional functions they give you. From page 131 in their guide:

TAdvStringGrid also provides a set of functions to allow performing the mapping of real cell indexes to visible cells indexes and vice versa. This is provided through:

function RealRowIndex(ARow: Integer): Integer;
function RealColIndex(ACol: Integer): Integer;

Returns the real column or row index for a given visible column or row index

function DisplRowIndex(ARow: Integer): Integer;
function DisplColIndex(ACol: Integer): Integer;

Returns the visible column or row index for a given real column or row index.

So I think you might find what you are looking for by changing your last method to:

procedure TForm1.BtnSelectCellClick(Sender: TObject);
begin
  AdvStringGrid1.SelectCells(DisplColIndex(MyCol),DisplRowIndex(MyRow),DisplColIndex(MyCol),DisplRowIndex(MyRow));
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top