Author: Tomas Rutkauskas
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.
Answer:
Maybe this helps:
1 unit Unit1;
2 3 interface4 5 uses6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids;
7 8 type9 TForm1 = class(TForm)
10 StringGrid1: TStringGrid;
11 procedure StringGrid1DblClick(Sender: TObject);
12 procedure StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
13 Shift: TShiftState; X, Y: Integer);
14 private15 {Private declarations}16 public17 {Public declarations}18 CurRow: integer;
19 CurCol: integer;
20 end;
21 22 var23 Form1: TForm1;
24 25 implementation26 27 {$R *.DFM}28 29 procedure TForm1.StringGrid1DblClick(Sender: TObject);
30 begin31 ShowMessage('Moving the topleft cell');
32 {Select the upper left cell}33 with StringGrid1 do34 begin35 Row := 1;
36 Col := 1;
37 end;
38 {Process all pending messages}39 Application.ProcessMessages;
40 {Wait 3 seconds}41 Sleep(3000);
42 {Select the previously selected cell}43 with StringGrid1 do44 begin45 Row := CurRow;
46 Col := CurCol;
47 end;
48 Application.ProcessMessages;
49 end;
50 51 procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
52 Shift: TShiftState; X, Y: Integer);
53 begin54 {Remember the selected row and column}55 CurRow := StringGrid1.Row;
56 CurCol := StringGrid1.Col;
57 end;
58 59 end.