本文主要是介绍【WinForm+DevExpress】GridView 在两个表格之间拖拽行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果图
可以将左边的行数据拖拽到右边,或者是将右边的行数据拖拽到左边
实现:
第一步:
新加两个GridControl,为他们设置数据源,并且将两个GridControl的AllowDrop属性设置为True
private void DragDemo_Load(object sender, EventArgs e){FillData();}
private void FillData(){//左边的网格默认查出tb_order表中的所有数据string gridView1sql1 = "select orderName from tb_order";//右边的网格默认不显示数据string gridView1sql2 = "select orderName from tb_order where 1=2";gridControl1.DataSource = GetDataSource(gridView1sql1);gridControl2.DataSource = GetDataSource(gridView1sql2);}}private DataTable GetDataSource(string sql){SqlDataAdapter da = new SqlDataAdapter(sql, DBConst.DBConnectionString);DataTable dt = new System.Data.DataTable();da.Fill(dt);return dt;}
第二步:添加四个方法:
//鼠标拖拽过程事件private void gridControl_DragOver(object sender, DragEventArgs e){e.Effect = DragDropEffects.Move;GridControl grid = sender as GridControl;if (grid == null) return;GridView view = grid.FocusedView as GridView;//GridView如果有分组,则展开所有分组数据if (view.GroupCount != 0)view.ExpandAllGroups();}//鼠标按下事件private void gridView_MouseDown(object sender, MouseEventArgs e){GridView view = sender as GridView;downHitInfo = null;GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));//鼠标左键按下去时在GridView中的坐标if (Control.ModifierKeys != Keys.None) return;if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0)downHitInfo = hitInfo;}//鼠标移动事件private void gridView_MouseMove(object sender, MouseEventArgs e){GridView view = sender as GridView;if (e.Button == MouseButtons.Left && downHitInfo != null)//不是左键则无效且鼠标左键按下去时需要在gridview区域中{Size dragSize = SystemInformation.DragSize;Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);if (!dragRect.Contains(new Point(e.X, e.Y))){object row = view.GetRow(downHitInfo.RowHandle);if (row == null) return;view.GridControl.DoDragDrop(row, DragDropEffects.Move);downHitInfo = null;DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true;}}}//拖拽完成后事件private void gridControl_DragDrop(object sender, DragEventArgs e){GridControl grid = sender as GridControl;DataTable table = grid.DataSource as DataTable;//获取选中行的数据DataRow row = ((DataRowView)e.Data.GetData(typeof(DataRowView))).Row;if (row != null && table != null && row.Table != table){table.ImportRow(row);row.Delete();}}
第三步:为两个GridControl绑定这4个方法
this.gridControl1.DragDrop += new System.Windows.Forms.DragEventHandler(this.gridControl_DragDrop);this.gridControl1.DragOver += new System.Windows.Forms.DragEventHandler(this.gridControl_DragOver);this.gridView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.gridView_MouseDown);this.gridView1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.gridView_MouseMove);
this.gridView2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.gridView_MouseDown);this.gridView2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.gridView_MouseMove);this.gridControl2.DragDrop += new System.Windows.Forms.DragEventHandler(this.gridControl_DragDrop);this.gridControl2.DragOver += new System.Windows.Forms.DragEventHandler(this.gridControl_DragOver);
注意:
如果只需要实现左边的表格向右边的表格拖拽功能,只需要给gridView1添加MouseDown和MouseMove事件,给gridControl2添加DragDrop和DragOver事件
GitHub地址:
https://github.com/xiaoyonglu/DevExpressDemos
这篇关于【WinForm+DevExpress】GridView 在两个表格之间拖拽行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!