//这只是解决你的问题的思路,更加祥细的控制你再自己做吧。
//你随便用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.