unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB, Grids, DBGridEh, ComCtrls, ToolWin, StdCtrls;
type
TForm1 = class(TForm)
TabControl1: TTabControl;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ADOConnection1: TADOConnection;
DBGridEh1: TDBGridEh;
DataSource1: TDataSource;
ADOQuery1: TADOQuery;
Edit1: TEdit;
Edit2: TEdit;
//调试中用,没有实际作用
procedure FormCreate(Sender: TObject);
procedure TabControl1Change(Sender: TObject);
procedure DBGridEh1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure DBGridEh1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TabControl1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure TabControl1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TabControl1StartDrag(Sender: TObject;
var DragObject: TDragObject);
procedure DBGridEh1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
//记录拖动起始和结束的行、列、tabindex值
//行列分别指记录在整个表(相对tabindex)中的记录号(1,2...)、列号(0,1,...)
//tabindex:0,1,2分别指A,B,C三个场地
//---------------------------------------------
//根据这几个值可以确定单个记录移动的情况,
//以及不同记录中的某几个字段
//---------------------------------------------
start_row,start_col,end_row,end_col,start_tab,end_tab:Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
with adoquery1 do
begin
close;
sql.clear;
sql.add('select * from table1');
open;
end;
tabcontrol1.TabIndex:=0;
tabcontrol1change(application);
end;
procedure TForm1.TabControl1Change(Sender: TObject);
begin
with adoquery1 do
begin
filter:='field1='+chr(39)+chr(65+tabcontrol1.tabindex)+chr(39);
filtered:=true;
end;
end;
procedure TForm1.DBGridEh1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
DG : TDBGrideh;
GC : TGridCoord;
CurRow : Integer;
begin
DG := Sender as TDBGrideh;
GC := DG.MouseCoord(X,Y);
//须判断拖动结束位置
//赋值变量end_row,end_col,end_tab
//根据start_row,start_col,start_tab 及 end_row,end_col,end_tab判断是否可以移动
//
with DG.DataSource.DataSet do
begin
with (Source as TDBGrideh).DataSource.DataSet do
Caption := 'You dragged "'+Fields[tGC.X-1].AsString+'"';
DisableControls;
CurRow := DG.Row;
MoveBy(GC.Y-CurRow);
Caption := Caption+' to "'+Fields[GC.X-1].AsString+'"';
MoveBy(CurRow-GC.Y);
EnableControls;
end;
end;
procedure TForm1.DBGridEh1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
GC : TGridCoord;
begin
GC := (Sender as TDBGrideh).MouseCoord(X,Y);
//允许拖动条件
Accept := Source is TDBGrideh and (GC.X > 0) and (GC.Y > 0);
//滚动,-----测试---成功,但
//y-dbgrideh1.height-100中的100可以根据屏幕大小改
//同样横向移动中的x>dbgrideh1.width-50 根据屏幕大小改
edit1.Text :=inttostr(dbgrideh1.height)+' '+inttostr
;
if y>dbgrideh1.height-100 then
adoquery1.Next;
if x<dbgrideh1.Top +100 then
adoquery1.Prior;
edit2.Text :=inttostr(dbgrideh1.width)+' '+inttostr(x);
if (x>dbgrideh1.Width-50) and (dbgrideh1.SelectedIndex<19) then
dbgrideh1.selectedindex:=dbgrideh1.SelectedIndex+1;
if (x<50) and (dbgrideh1.selectedindex>1) then
dbgrideh1.selectedindex:=dbgrideh1.SelectedIndex-1;
end;
procedure TForm1.DBGridEh1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
DG : TDBGrideh;
tgc:tgridcoord;
begin
DG := Sender as TDBGrideh;
TGC := DG.MouseCoord(X,Y);
start_row:=adoquery1.RecNo;
start_col:=dbgrideh1.SelectedIndex ;
start_tab:=tabcontrol1.TabIndex;
// showmessage(inttostr(start_col));
//允许拖动条件
if (tGC.X > 0) and (tGC.Y > 0) then
(Sender as TDBGrideh).BeginDrag(False);
end;
end.