读书人

求救~DragEvent!该怎么解决

发布时间: 2012-01-01 23:10:55 作者: rapoo

求救~DragEvent!!
我在datagrid1中,选中一行,进行拖放。在它的MouseMove()事件里调用DoDragDrop(),如果把它拖到datagrid2中,那么我在datagrid2的DragDrop()可以执行我的代码,一切正常。如果鼠标一直都在datagrid1里,好像一直处于拖放的状态,重新选择不能选中行,这个时候datagrid的MouseDown(),MouseUp()事件都不能触发,而且也不能执行DragDrop(),我想知道怎么才能把拖放状态取消掉。因为如果用户本来想拖把一行从datagrid1拖到datagrid2中,并且已经开始拖了,但是突然发现选错了一行,想换行,结果就不能换行了,因为不能选中。把我的代码贴出来给大家看看。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace ICYOS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Point _ptStartDrag;
int _dragRow;

DataSet dsc1Dbg2=new DataSet();
OracleDataAdapter c1dgbAdp=new OracleDataAdapter();


private void Form1_Load(object sender, EventArgs e)
{
string selectStr = "select * from kf_clientinfo";
DataSet myDs = DBControl.GetData(selectStr);//执行sql
c1TrueDBGrid1.DataSource = myDs.Tables[0];


selectStr = "select * from kf_clientinfo1";
DBControl.updatedata(c1dgbAdp, selectStr, dsc1Dbg2);)//执行sql
c1TrueDBGrid2.DataSource = dsc1Dbg2.Tables[0];
}

private void c1TrueDBGrid1_MouseDown(object sender, MouseEventArgs e)
{
int row, col;
this._ptStartDrag = Point.Empty;
this._dragRow = -1;
if (this.c1TrueDBGrid1.CellContaining(e.X, e.Y, out row, out col))
{
// Save the starting point of the drag operation.
this._ptStartDrag = new Point(e.X, e.Y);
this._dragRow = row;
}


}

private void c1TrueDBGrid1_MouseMove(object sender, MouseEventArgs e)
{

// If we don't have an empty start drag point, then the drag has been initiated.
if (!this._ptStartDrag.IsEmpty)
{
// Create a rectangle that bounds the start of the drag operation by 2 pixels.
Rectangle r = new Rectangle(this._ptStartDrag, Size.Empty);
r.Inflate(2, 2);

// If we've moved more than 2 pixels, start the drag operation.
if (!r.Contains(e.X, e.Y))
{

this.c1TrueDBGrid1.Row = this._dragRow;
this.c1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightRow;
this.c1TrueDBGrid1.DoDragDrop(this._dragRow, DragDropEffects.Copy);
c1TrueDBGrid1.Row = 0;
}
}

}
private void c1TrueDBGrid2_DragDrop(object sender, DragEventArgs e)
{
try
{
int row = (int)e.Data.GetData(typeof(int));

// Use the grid's indexer to get some data.
string codeclient = this.c1TrueDBGrid1[row, "codeclient"].ToString();


// Add a new row to the data set for the bottom grid.
DataRowView drv = dsc1Dbg2.Tables[0].DefaultView.AddNew();
drv["codeclient"] = codeclient;
drv.EndEdit();


this.c1TrueDBGrid2.MoveLast();
this.c1TrueDBGrid2.Select();

// Commit changes to the database.
DataSet inserted = this.dsc1Dbg2.GetChanges(DataRowState.Added);
if (!(inserted == null))
{
this.c1dgbAdp.Update(inserted);
}
ResetDragDrop();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}

}

private void c1TrueDBGrid2_DragEnter(object sender, DragEventArgs e)
{
this.label3.Text = "Create a new record when dropped...";
e.Effect = DragDropEffects.Copy;
}

private void ResetDragDrop()
{
// Turn off drag-and-drop by resetting the highlight and label text.
this._ptStartDrag = Point.Empty;
this._dragRow = -1;
this.c1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder;
this.c1TrueDBGrid2.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder;
this.label3.Text = "Drag a row from the top grid and drop it on the bottom grid.";

}

}
}

[解决办法]
需要增加MouseUp事件,在里面增加

C# code
                        this._ptStartDrag   =   Point.Empty;                         this._dragRow   =   -1;
[解决办法]
回去想了一下,感觉用MouseUp事件不大适合,在MouseDown里判断鼠标左键或者右键,右键的话再
this._ptStartDrag = Point.Empty;
this._dragRow = -1;
会好点

[解决办法]
C# code
                private   void   c1TrueDBGrid1_MouseMove(object   sender,   MouseEventArgs   e)                 {                 if ((e.Button & MouseButtons.Left) == MouseButtons.Left)                   {                        //   If   we   don't   have   an   empty   start   drag   point,   then   the   drag   has   been   initiated.                         if   (!this._ptStartDrag.IsEmpty)                         {                                 //   Create   a   rectangle   that   bounds   the   start   of   the   drag   operation   by   2   pixels.                                 Rectangle   r   =   new   Rectangle(this._ptStartDrag,   Size.Empty);                                 r.Inflate(2,   2);                                 //   If   we've   moved   more   than   2   pixels,   start   the   drag   operation.                                 if   (!r.Contains(e.X,   e.Y))                                 {                                                                                 this.c1TrueDBGrid1.Row   =   this._dragRow;                                         this.c1TrueDBGrid1.MarqueeStyle   =   C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightRow;                                         this.c1TrueDBGrid1.DoDragDrop(this._dragRow,   DragDropEffects.Copy);                                         c1TrueDBGrid1.Row   =   0;                                 }                         }                   }                } 

读书人网 >C#

热点推荐