Refocus a cell in a TStringGrid (Views: 100)


I have a stringgrid which displays spreadsheet type figures for a year. When the user double clicks a cell then I take the value from one of the cells in that same row and bring up another DB based form so the user can work on data for that record. Now when they have finished editing that record, I need to repopulate the stringgrid, which could (and probably will) put the initial row in a different position. This now leaves a couple of problems: 1. I have a highlighted cell which doesn't correspond to anything. 2. The user doesn't know immediately which item they have just been working on. How can I search the string grid for the item that was initially selected (one of the cells in the selected row holds the primary key value)? When this row is found, I then need to highlight the first column of that row. Is the above possible? I can't seem to see any properties to achieve it.


Maybe this helps:

unit Unit1;


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

  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1DblClick(Sender: TObject);
    procedure StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    {Private declarations}
    {Public declarations}
    CurRow: integer;
    CurCol: integer;

  Form1: TForm1;


{$R *.DFM}

procedure TForm1.StringGrid1DblClick(Sender: TObject);
  ShowMessage('Moving the topleft cell');
  {Select the upper left cell}
  with StringGrid1 do
    Row := 1;
    Col := 1;
  {Process all pending messages}
  {Wait 3 seconds}
  {Select the previously selected cell}
  with StringGrid1 do
    Row := CurRow;
    Col := CurCol;

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  {Remember the selected row and column}
  CurRow := StringGrid1.Row;
  CurCol := StringGrid1.Col;


<< Back to main page