• 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;

  • 相关阅读:
    Python之路(第十七篇)logging模块
    Python之路(第十五篇)sys模块、json模块、pickle模块、shelve模块
    Python之路(第十四篇)os模块
    Python之路(第十三篇)time模块、random模块、string模块、验证码练习
    Python之路(第十二篇)程序解耦、模块介绍导入安装、包
    Python编程笔记(第三篇)【补充】三元运算、文件处理、检测文件编码、递归、斐波那契数列、名称空间、作用域、生成器
    Python之路(第十一篇)装饰器
    Python之路(第十篇)迭代器协议、for循环机制、三元运算、列表解析式、生成器
    Python之路(第九篇)Python文件操作
    Python编程笔记(第二篇)二进制、字符编码、数据类型
  • 原文地址:https://www.cnblogs.com/jijm123/p/8087933.html
Copyright © 2020-2023  润新知