使用DBGrid中的SelectIndex 和PopupMenu1结合,先在DBGrid中显示,再通过右键选择删除。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, StdCtrls, ExtCtrls, Menus, DB;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Panel1: TPanel;
Button1: TButton;
PopupMenu1: TPopupMenu;
Del1: TMenuItem;
procedure FormActivate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure DBGrid1KeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
procedure Add1Click(Sender: TObject);
procedure Del1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure DeleteRS(DataSet: TDataSet);
implementation
uses UDM;
{$R *.dfm}
procedure DeleteRS(DataSet: TDataSet);
begin
try
with DataSetdo
begin
if not IsEmpty then
Delete;
end;
except
Abort;
end;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
if not DM.dstData.Active then
DM.dstData.Open;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if DM.dstData.Active then
DM.dstData.Close;
end;
procedure TForm1.DBGrid1KeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
var
LastEditCol:integer;
begin
LastEditCol:=DbGrid1.SelectedIndex;
if LastEditCol <>2 then
exit;//让浮动式菜单只对第二列起作用
if(Key=VK_RETURN)//VK_***是您的热键的虚键值
then
PopupMenu1.Popup(336,222);//336,222浮动式菜单显示的位置
end;
procedure TForm1.Add1Click(Sender: TObject);
begin
with DBGrid1.DataSource.DataSetdo
begin
AppendRS(DBGrid1.DataSource.DataSet);
end;
end;
procedure TForm1.Del1Click(Sender: TObject);
begin
with DBGrid1.DataSource.DataSetdo
begin
if DBGrid1.SelectedIndex <> 0 then
if not IsEmpty then
begin
DeleteRS(DBGrid1.DataSource.DataSet);
end;
end;
end;
end.