Link to home
Start Free TrialLog in
Avatar of Mike Littlewood
Mike LittlewoodFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Stringgrid visible selection not the same as actual row selected

Ok hopefully Im missing something obvious but I cant seem to spot it.

Im populating a stringgrid with a number of records (set to rowselect)
The user can then double click a row and open up another form to view more information.

When the user closes the second form it clears the previous records in the stringgrid and repopulates it so that the selected record is now moved to the top of the list.
Im calling the OnSelectCell event of the stringgird to auto select the top row (ie the record they just viewed).
My problem is the actual visual row selected still shows at the last position double clicked.

ie if the user double clicks row 5 and opens up the second form then closed, I call SelectCell of the first row various settings change to show that the first row is selected, but the visible area shown still shows row 5.

Am I missing something?
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Do I have to call the Stringgrid.Selection function to show the visibility?

Or do I have to call the OnClick function first?
hey mike,

here's my test code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i,j:integer;
begin
  for j:=1 to StringGrid1.ColCount do
    stringgrid1.Cols[j].Clear;
  stringgrid1.RowCount:=51;
  for i:=1 to 50 do
    for j:=1 to StringGrid1.ColCount do
      stringgrid1.Cols[j-1].Add(inttostr(j)+inttostr(i))
end;

procedure TForm1.StringGrid1DblClick(Sender: TObject);
begin
  StringGrid1.TopRow:=StringGrid1.Row;// this?
end;

end.

works fine. I am not sure if I udnerstood your request though. Is that what you were looking for?
Not quite Ciuly, but I think I have found a work around.

Imagine 10 rows in a stringgrid.
I double click row 5 (it row selects and then opens up another form based on some details in that row)
I then close the second form and repopulate the 10 rows again as changes may have occured with records.
When I repopulate the grid it reorders them fine so that the previously double clicked record is now at the top.

Problem is that visually it still shows row 5 as being currently selected, even though I have told the application to select row 1.
The application has actually selected row 1 because other events on the form have modified certain menu properties, its just the visual element.
Its a bit strange I know, but I used a work around of setting Stringgrid1.Selection := TGridRect of top row.
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeah your way worked as well Ciuly so Ill give you the points.
might alter the code later once I work out which is the best method.