Delphi about DBGrid Multi-select Delete (SHITF multiple selection, CTRL multiple Selection)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.