Delete a multi-select record
Procedure Thistoryform.deletebuttonclick (Sender:tobject);
Var
Tempbookmark:tbookmark;
I:integer;
Begin
If Dbgrid1. Selectedrows.count <= 0 Then
Begin
MessageBox (Handle, ' Please select the record you want to delete ', ' hint ', MB_OK);
Exit;
End
if MessageBox (Handle, ' deleted data will not be recoverable, are you sure you want to delete it? ', ' hint ', 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];//important here, get the bookmark
tempbookmark:= DBGrid1.Datasource.Dataset.GetBookmark;
dbgrid1.datasource.dataset.gotobookmark (TempBookmark);
with ADOQuery3 do
begin
Close;
sql. Clear;
sql. Add (' Delete from the his where id=:a ');
parameters.parambyname (' a '). Value:=dbgrid1.datasource.dataset.fieldbyname (' id '). Asinteger;
ExecSql;
end;
dbgrid1.datasource.dataset.freebookmark (TempBookmark); //Release Bookmark
inc (i); //Next Select Record
End;
DBGrid1.Datasource.Dataset.EnableControls;
act_showctinfo (lbname.caption);
Memo1.Clear;
end;
End;
Defining Global Variables
Var
Blselect:boolean;
Bookmark:tbookmark;
Currno, Oldno:integer;
Press SHIFT to select multiple
Procedure Thistoryform.dbgrid1mouseup (Sender:tobject;
Button:tmousebutton; Shift:tshiftstate; X, Y:integer);
Begin//Implement shift+ left mouse button click multiple selection
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 and 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
Delphi about DBGrid Multi-select Delete (SHITF multiple selection, CTRL multiple Selection)