Delphi 关于DBGrid多选删除(shitf多选,ctrl多选)
////删除多选记录
procedure THistoryForm.DeleteButtonClick(Sender: TObject);
var
tempBookMark:TbookMark;
i:integer;
begin
if dbgrid1.SelectedRows.Count <= 0 Then
begin
MessageBox(Handle,'请选择您要删除的记录','提示',MB_OK);
Exit;
end;
If MessageBox(Handle,'删除后数据将不可恢复,您确定要删除吗?','提示',mb_iconinformation+mb_YesNo)=IdYes Then
Begin
i:=0;
DBGrid1.Datasource.Dataset.DisableControls;
While i<=dbgrid1.SelectedRows.Count - 1 Do
Begin
DBGrid1.Datasource.Dataset.Bookmark:=dbgrid1.SelectedRows.Items[i];//重要的在这里,取得BOOKMARK
TempBookmark:=DBGrid1.Datasource.Dataset.GetBookmark;
DBGrid1.Datasource.Dataset.GotoBookmark(TempBookmark);
with ADOQuery3 do
begin
Close;
SQL.Clear;
SQL.Add('delete from his where id=:a');
Parameters.ParamByName('a').Value:=Dbgrid1.DataSource.DataSet.fieldbyname('id').AsInteger;
ExecSql;
end;
DBGrid1.Datasource.Dataset.FreeBookmark(TempBookmark); //释放BOOKMARK
inc(i); //下一个选择记录
End;
DBGrid1.Datasource.Dataset.EnableControls;
Act_ShowCTInfo(LBName.Caption);
Memo1.Clear;
end;
end;
//定义全局变量
var
blSelect: Boolean;
BookMark: TBookMark;
CurrNo, OldNo: integer;
/////////按Shift多选
procedure THistoryForm.DBGrid1MouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin //实现 Shift+鼠标左键单击 多选
if Button = mbLeft then
begin
if not blSelect then
begin
BookMark := ADOQuery1.GetBookMark;
OldNo := ADOQuery1.RecNo;
blSelect := True;
Exit;
end
else
begin
if ssShift in Shift then
begin
CurrNo := ADOQuery1.RecNo;
ADOQuery1.DisableControls;
ADOQuery1.GotoBookmark(BookMark);
DBGrid1.SelectedRows.CurrentRowSelected := True;
if CurrNo > OldNo then
begin
while CurrNo > ADOQuery1.RecNo do
begin
DBGrid1.SelectedRows.CurrentRowSelected := True;
ADOQuery1.Next;
end;
end
else
begin
while CurrNo < ADOQuery1.RecNo do
begin
DBGrid1.SelectedRows.CurrentRowSelected := True;
ADOQuery1.Prior;
end;
end;
ADOQuery1.EnableControls;
ADOQuery1.FreeBookmark(BookMark);
blSelect := False;
CurrNo := 0;
OldNo := 0;
end
else
begin
BookMark := ADOQuery1.GetBookMark;
OldNo := ADOQuery1.RecNo;
blSelect := True;
Exit;
end;
end;
end;
end;