1

如何编写在 DBGrid.SelectedRows.Count 更改时触发的自定义事件?

当 DBGrid 中的选定行为 [零 | 时,我需要此事件有条件地显示/隐藏面板 一个] 或多个。

从现在开始,我使用以下代码,但 IMO 编码自定义事件在这里更合适:

TForm3.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; Field: TField; State: DBGridDrawState);
begin 
  Panel1.Visible := TDBGrid(Sender).SelectedRows.Count > 1; 
end; 
4

1 回答 1

2

要捕获更改内部书签列表的所有事件,您必须覆盖

  1. 链接活动
  2. 按键
  3. 鼠标按下

上面的示例就像插入器类一样,可以更改为新组件。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,  Grids, DBGrids, DB, ADODB;

type
  TDBGrid=Class(DBGrids.TDBGrid)
  private
    FOnSelectionChanged: TNotifyEvent;
    procedure LinkActive(Value: Boolean); override;
    procedure KeyDown(var Key: Word; Shift: TShiftState);override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); override;
  published
    published
    property OnSelectionChanged:TNotifyEvent read  FOnSelectionChanged write FOnSelectionChanged;
  End;

  TForm1 = class(TForm)
    ADODataSet1: TADODataSet;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    procedure MyOnSelectionChanged(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation
uses unit3;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  AdoDataset1.Active := Not  AdoDataset1.Active;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGrid1.OnSelectionChanged := MyOnSelectionChanged;
end;

procedure TForm1.MyOnSelectionChanged(Sender: TObject);
begin
   Caption := IntToStr(TDBGrid(Sender).SelectedRows.Count);

end;

{ TDBGrid }

procedure TDBGrid.KeyDown(var Key: Word; Shift: TShiftState);

begin
  inherited;
  if Assigned(FOnSelectionChanged) then FOnSelectionChanged(self);

end;

procedure TDBGrid.LinkActive(Value: Boolean);
begin
  inherited;
  if Assigned(FOnSelectionChanged) then FOnSelectionChanged(self);

end;

procedure TDBGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited;
  if Assigned(FOnSelectionChanged) then FOnSelectionChanged(self);

end;

end.
于 2013-07-15T15:19:56.843 回答