dbgrid多选记录问题求救???

rwq_ 2000-11-24 07:49:00
在DELPHI数据库运行程序中,我们如何通过配合SHIFT键在DBGRID中连选多条记录(就像在资源管理器中通过配合SHIFT键连选多个文件夹)???????
...全文
267 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rwq_ 2001-08-02
  • 打赏
  • 举报
回复
哈哈,谢谢你了,够朋友,有义气!
王集鹄 2001-07-28
  • 打赏
  • 举报
回复
//呵!呵!呵!呵!呵!呵!
//真的要我写出来呀!!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables, Grids, DBGrids;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DBGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DataSource1DataChange(Sender: TObject; Field: TField);
procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FKeyShift: Boolean;
FRecNo: Integer;
FOldNo: Integer;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
FKeyShift := ssShift in Shift;
end;

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

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
FOldNo := FRecNo;
FRecNo := TDataSource(Sender).DataSet.RecNo;
if FKeyShift then DBGrid1.SelectedRows.CurrentRowSelected := True;
FKeyShift := False;
end;

procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
I: Integer;
begin
if FKeyShift then begin
if -1 in [FOldNo, FRecNo] then Exit;
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;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FRecNo := -1;
end;

end.


object Form1: TForm1
Left = 192
Top = 107
Width = 544
Height = 287
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object DBGrid1: TDBGrid
Left = 0
Top = 0
Width = 536
Height = 260
Align = alClient
DataSource = DataSource1
Options = [dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgRowSelect, dgConfirmDelete, dgCancelOnExit, dgMultiSelect]
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
OnKeyDown = DBGrid1KeyDown
OnKeyUp = DBGrid1KeyUp
OnMouseUp = DBGrid1MouseUp
end
object DataSource1: TDataSource
DataSet = Table1
OnDataChange = DataSource1DataChange
Left = 112
Top = 8
end
object Table1: TTable
Active = True
DatabaseName = 'DBDEMOS'
TableName = 'biolife.db'
Left = 144
Top = 8
end
end
rwq_ 2000-11-25
  • 打赏
  • 举报
回复
zswang :ctrl键当然可以,但是不能连选,只能一条一条选择,我要的是如何是通过写代码设置某一条记录为被处于选择状态!
王集鹄 2000-11-24
  • 打赏
  • 举报
回复
//pas
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables, Grids, DBGrids;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Table1: TTable;
DataSource1: TDataSource;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
with DBGrid1.DataSource.DataSet do begin
First;
while not Eof do begin
if DBGrid1.SelectedRows.CurrentRowSelected then
ShowMessage(FieldByName('我不知道你的字段是什么').AsString);
Next;
end;
end;
end;

end.

//dfm
object Form1: TForm1
Left = 192
Top = 107
Width = 544
Height = 363
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object DBGrid1: TDBGrid
Left = 0
Top = 0
Width = 536
Height = 299
Align = alTop
DataSource = DataSource1
Options = [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgMultiSelect]
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
end
object Button1: TButton
Left = 216
Top = 304
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Table1: TTable
Active = True
DatabaseName = 'SY_JXC'
TableName = '我不知道你的表名是什么.DB'
Left = 104
Top = 16
end
object DataSource1: TDataSource
DataSet = Table1
Left = 56
Top = 16
end
end
//请按Ctrl吧,要按Shift自己编
nononono 2000-11-24
  • 打赏
  • 举报
回复
在OnCellClick事件中检查Shift的状态,然后对区域中的记录的选择状态取反。
nononono 2000-11-24
  • 打赏
  • 举报
回复
DBGRID1->Options->dgMultiSelect = true;
nononono 2000-11-24
  • 打赏
  • 举报
回复
但好象不直接支持SHIFT,大概应该写段代码。

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧