DBGrid中Shift多选

Posted 癫狂编程

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DBGrid中Shift多选相关的知识,希望对你有一定的参考价值。

[dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, 
dgTabs, dgRowSelect, dgConfirmDelete, dgCancelOnExit, dgMultiSelect]

相关代码
procedure TFamEditPeople.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  FKeyShift := ssShift in Shift;
end;

procedure TFamEditPeople.DBGrid1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  FKeyShift := False;
end;

procedure TFamEditPeople.DataSource1StateChange(Sender: TObject);
begin
  FOldNo := FRecNo;
  if TDataSource(Sender).DataSet.Active then
    FRecNo := TDataSource(Sender).DataSet.RecNo;
  FKeyShift := False;
end;

procedure TFamEditPeople.DBGrid1MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  I: Integer;
begin
  if Button <> mbLeft then
    Exit;
  if not TDBGrid(Sender).DataSource.DataSet.Active then
    Exit;
  if FKeyShift then begin
    FRecNo := TDBGrid(Sender).DataSource.DataSet.RecNo;
    if FKeyShift then DBGrid1.SelectedRows.CurrentRowSelected := True;
    if FOldNo = -1 then
      FOldNo := 1;
    if FRecNo > FOldNo then
      for I := FRecNo downto FOldNo do begin
        TDBGrid(Sender).DataSource.DataSet.RecNo := I;
        TDBGrid(Sender).SelectedRows.CurrentRowSelected := True;
      end
    else
      for I := FRecNo to FOldNo do begin
        TDBGrid(Sender).DataSource.DataSet.RecNo := I;
        TDBGrid(Sender).SelectedRows.CurrentRowSelected := True;
      end;
  end
  else//Shift not pressed
    FOldNo := TDBGrid(Sender).DataSource.DataSet.RecNo
end; 



实现DBGrid Shift+鼠标左键单击 多选 
关键字: DBGrid Shift 多选 
分类 : 个人专区 
密级 : 公开 
(评分: , 回复: 0, 阅读: 96) &raquo;&raquo; 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes,
  Graphics, Controls, Forms, Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls;

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    ADOConnection1: TADOConnection;
    DataSource1: TDataSource;
    Button1: TButton;
    ADOTable1: TADOTable;
    procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    blSelect: Boolean;
    BookMark: TBookMark;
    CurrNo, OldNo: integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin //实现 Shift+鼠标左键单击 多选 
  if Button = mbLeft then
  begin
    if not blSelect then
    begin
      BookMark := ADOTable1.GetBookMark;
      OldNo := ADOTable1.RecNo;
      blSelect := True;
      Exit;
    end
    else
    begin
      if ssShift in Shift then
      begin
        CurrNo := ADOTable1.RecNo;
        ADOTable1.DisableControls;
        ADOTable1.GotoBookmark(BookMark);
        DBGrid1.SelectedRows.CurrentRowSelected := True;
        if CurrNo > OldNo then
        begin
          while CurrNo > ADOTable1.RecNo do
          begin
            DBGrid1.SelectedRows.CurrentRowSelected := True;
            ADOTable1.Next;
          end;
        end
        else
        begin
          while CurrNo < ADOTable1.RecNo do
          begin
            DBGrid1.SelectedRows.CurrentRowSelected := True;
            ADOTable1.Prior;
          end;
        end;
        ADOTable1.EnableControls;
        ADOTable1.FreeBookmark(BookMark);
        blSelect := False;
        CurrNo := 0;
        OldNo := 0;
      end
      else
      begin
        BookMark := ADOTable1.GetBookMark;
        OldNo := ADOTable1.RecNo;
        blSelect := True;
        Exit;
      end;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ADOConnection1.Connected := true;
  ADOTable1.Close;
  ADOTable1.TableName := Table_Name; //表名
  ADOTable1.Open;
  DBGrid1.Options := DBGrid1.Options + [dgMultiSelect]; //开启多选
end;

end.

//另外,Shift+小键盘UP/DOWN 也可以实现多选  

 

以上是关于DBGrid中Shift多选的主要内容,如果未能解决你的问题,请参考以下文章

如何在DBGrid里实现Shift+“选择行”区间多选的功能!

dbgrid多选日记

怎样实现在DBGrid中双击选择整行,并且可以多选?谢谢!!

delphi7 DBGrid 中怎么样使用多选删除二张不同的表?

delphi6,能否在DBGrid组件中,对某一列的单元格,添加下拉菜单,实现多选功能

Delphi DBGrid记录移动问题!