2004. március 4., csütörtök

Refocus a cell in a TStringGrid


Problem/Question/Abstract:

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:

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

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

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

end.

Nincsenek megjegyzések:

Megjegyzés küldése