(300分)在一个dbgrid中进行字段的拖拽时,如何得到释放鼠标右键时,该记录的信息?在线等...(300分)

  • 主题发起人 主题发起人 boysjp2000
  • 开始时间 开始时间
B

boysjp2000

Unregistered / Unconfirmed
GUEST, unregistred user!
我只做一个使用dbgrid控件进行字段拖拽时遇到以下困难:
1:我首先在dbgrid控件的mousemove事件中设置拖拽时的鼠标指针即使用begindrag(false,10);
2:我在dbgrid控件的dragover事件中设置accept=true;
3: 我如何使用dbgrid控件的DragDrop事件,换句话说就是在释放鼠标时如
何将已拖拽的数据移动到释放鼠标时的当前记录栏内,我无法知道释放鼠标是在那个字段和那个记录,
因为进行数据拖拽时,只用被拖拽的数据是选中状态
 
申明一个消息处理函数,响应mouse右键up的消息
 
鼠标移动时将鼠标下的当前记录选择,释放时读被选择的记录 ok?
 
这么有名的论坛难道就没有热心人来吗??????????
气愤--愤怒--可恶。。。。。。。
 
这是 dxdbgrid的例子

procedure TMainForm.GridDragDrop(Sender, Source: TObject; X, Y: Integer);
var
Node: TdxTreeListNode;
I, J: Integer;
AFieldName: string;
AValue: Variant;
SourceGrid, TargetGrid: TdxDBGrid;
SourceData, TargetData: TDataSet;
begin
SourceGrid := Source as TdxDBGrid;
TargetGrid := Sender as TdxDBGrid;

Node := TargetGrid.GetNodeAt(X, Y);
if Node = nil then Exit;
while Node.HasChildren do
Node := Node.Items[0];

SourceData := SourceGrid.DataSource.DataSet;
TargetData := TargetGrid.DataSource.DataSet;
SourceData.DisableControls;
TargetData.DisableControls;
try
for I := 0 to SourceGrid.SelectedCount - 1 do
begin
SourceData.Bookmark := SourceGrid.SelectedRows;
SourceData.Edit;
for J := 0 to TargetGrid.GroupColumnCount - 1 do
begin
AFieldName := TargetGrid.GroupColumns[J].FieldName;
AValue := Node.Values[TargetGrid.GroupColumns[J].Index];
SourceData[AFieldName] := AValue; // change values according to the target group
end;
// Y or N according to the TargetGrid
SourceData['Customer'] := TargetData['Customer'];
SourceData.Post;
end;
SourceGrid.ClearSelection;
finally
SourceData.EnableControls;
TargetData.EnableControls;
end;
end;

procedure TMainForm.GridDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
// Both Sender and Source are TdxDBGrid instances
Accept := (Source is TdxDBGrid) and (Sender is TdxDBGrid);
// If Sender = Source and it does not have grouping columns,
// then Accept := False, i.e. reorder of nodes is not implemented
Accept := Accept and ((Source <> Sender) or (TdxDBGrid(Source).GroupColumnCount > 0));
// Drag&Drop of entire group is not implemented
Accept := Accept and not TdxDBGrid(Source).SelectedNodes[0].HasChildren;
end;
 
//这只是解决你的问题的思路,更加祥细的控制你再自己做吧。
//你随便用ADO连个数据源就可以了。
unit Unit1;

interface

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

type
TForm1 = class(TForm)
ADOTable1: TADOTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Edit1: TEdit;
procedure DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure DBGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
FCanDrag : Boolean;
FHoldData : Variant;
FSrcRow,FSrcCol : Integer;
FMouseX,FMouseY : Longint;
public

end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := true;
end;

procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FCanDrag then
dbGrid1.begindrag(false,10);
FCanDrag := false;
end;

procedure TForm1.DBGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
R : TGridCoord;
BK : TBookmark;
begin
R := dbGrid1.MouseCoord(X, Y);
edit1.Text := 'drop at x='+IntToStr(R.x)+' '+IntToStr(R.y)+' FieldName='+dbGrid1.Columns[R.x-1].FieldName;
if R.Y=FSrcRow then exit;

if R.X<>FSrcCol then
begin
showmessage('field name not the same');
exit;
end;

BK := dbGrid1.DataSource.DataSet.GetBookmark;
dbGrid1.DataSource.DataSet.MoveBy(R.Y-FSrcRow);
dbGrid1.DataSource.DataSet.Edit;
dbGrid1.DataSource.DataSet.FieldByName(dbGrid1.Columns[R.x-1].FieldName).Value := FHoldData;
dbGrid1.DataSource.DataSet.Post;
dbGrid1.DataSource.DataSet.GotoBookmark(BK);
dbGrid1.DataSource.DataSet.FreeBookmark(BK);
end;

procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
R : TGridCoord;
begin
R := dbGrid1.MouseCoord(X, Y);
FSrcRow := R.Y;
FSrcCol := R.X;
FHoldData := dbGrid1.Columns[FSrcCol-1].Field.Value;
FCanDrag := true;
end;

end.
 
DFM

object Form1: TForm1
Left = 192
Top = 107
Width = 696
Height = 480
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 = 16
Top = 64
Width = 649
Height = 361
DataSource = DataSource1
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
OnDragDrop = DBGrid1DragDrop
OnDragOver = DBGrid1DragOver
OnMouseMove = DBGrid1MouseMove
OnMouseUp = DBGrid1MouseUp
end
object Edit1: TEdit
Left = 144
Top = 8
Width = 353
Height = 21
TabOrder = 1
Text = 'Edit1'
end
object ADOTable1: TADOTable
CursorType = ctStatic
TableName = 'CSCODE'
Left = 48
Top = 16
end
object DataSource1: TDataSource
DataSet = ADOTable1
Left = 88
Top = 16
end
end
 
刚接到你的短讯,就试了一下。
别那么过激吧。
我很久没在DFW发表什么了,本人水平也很是一般!
但是,DFW上还是龙虎之地。
 
一个简单的例子,不知道对不对你的味口

procedure TForm1.Edit1DragDrop(Sender, Source: TObject; X, Y: Integer);
var f :integer;
begin
if (Sender is TEdit) and (Source is TDBGrid) then
begin
with Sender as TEdit do
begin
Text := '';
for f:=0 to DBGrid1.FieldCount-1 do
Text := DBGrid1.Fields[f].AsString +' '+Text;
end;
end;

end;

作用:把一行数据放到一个Edit中,用空格分开。
 
使用标签,自己研究一下!简单!
不要什么都要现成的,自己解决了,才会提高!~
 
接受答案了.
 
后退
顶部