DBGrid中Shift多选
|
|
|
作者:佚名
来源:InterNet 加入时间:2007-1-26 |
[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) »» 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 也可以实现多选
[文章录入员:nancy] |
|
|
|
|