Stringgrid visible selection not the same as actual row selected

Posted on 2006-06-04
Last Modified: 2010-04-05
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?
Question by:mikelittlewood
    LVL 15

    Author Comment

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

    Or do I have to call the OnClick function first?
    LVL 28

    Expert Comment

    hey mike,

    here's my test code:

    unit Unit1;


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

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

      Form1: TForm1;


    {$R *.dfm}

    procedure TForm1.FormCreate(Sender: TObject);
    var i,j:integer;
      for j:=1 to StringGrid1.ColCount do
      for i:=1 to 50 do
        for j:=1 to StringGrid1.ColCount do

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


    works fine. I am not sure if I udnerstood your request though. Is that what you were looking for?
    LVL 15

    Author Comment

    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.
    LVL 28

    Accepted Solution

    well .. I guess it must have been something to do with the way you made the selection of top row.
    however, I think that the reverse:
    should also make the problem go away. (having in mind that the rows must not move (since they are already sorted and in the right place) and only the selection needs to change.
    I hope this time I got it right :)
    in any case, it's good that it's fixed.
    LVL 15

    Author Comment

    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.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now