dev GridControl实现拖拽
一、示例说明
以gridControl1和gridControl2为例,从gridControl1拖拽行到gridControl2中去。
二、属性设置
gridControl2.AllowDrop = true;
gridControl1.AllowDrop = true;
三、事件代码:分别为GridControl和gridview添加下列事件
#region Grid拖拽操作 GridHitInfo GriddownHitInfo = null; #region 拖出 private void gridView1_MouseDown(object sender, MouseEventArgs e) { GridView view = sender as GridView; GriddownHitInfo = null; GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y)); if (Control.ModifierKeys != Keys.None) return; if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0) { GriddownHitInfo = hitInfo; } } private void gridView1_MouseMove(object sender, MouseEventArgs e) { GridView view = sender as GridView; if (e.Button == MouseButtons.Left && GriddownHitInfo != null) { Size dragSize = SystemInformation.DragSize; Rectangle dragRect = new Rectangle(new Point(GriddownHitInfo.HitPoint.X - dragSize.Width / 2, GriddownHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize); if (!dragRect.Contains(new Point(e.X, e.Y))) { DataRow row = view.GetDataRow(GriddownHitInfo.RowHandle); if (row != null) { view.GridControl.DoDragDrop(row, DragDropEffects.Move); GriddownHitInfo = null; DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true; } } } } #endregion #region 拖入 private void gridControl2_DragOver(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(DataRow))) e.Effect = DragDropEffects.Move; else e.Effect = DragDropEffects.None; } private void gridControl2_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void gridControl2_DragDrop(object sender, DragEventArgs e) { try { DataRow dr = e.Data.GetData(typeof(DataRow)) as DataRow; if (dr != null) { FillTreeData(sender, dr); } } catch (Exception ex) { MessageBox.Show("系统提示", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); } } void FillTreeData(object sender, DataRow dr) { this.gridView2.AddNewRow(); gridView2.SetFocusedRowCellValue("id", Guid.NewGuid().ToString()); gridView2.SetFocusedRowCellValue("name", dr["name"].ToString()); gridView2.SetFocusedRowCellValue("code", dr["code"].ToString()); } #endregion #endregion
四、多行拖拽
1、属性
gridControl2.AllowDrop = true;
gridControl1.AllowDrop = true;
设置gridview可以多行选择: gridView1.OptionsSelection.MultiSelect = true;
2、事件代码 :所有的DataRow 变为 List<DataRow>
#region Grid拖拽操作 GridHitInfo GriddownHitInfo = null; #region 拖入 private void gridControl2_DragOver(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(List<DataRow>))) e.Effect = DragDropEffects.Move; else e.Effect = DragDropEffects.None; } private void gridControl2_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void gridControl2_DragDrop(object sender, DragEventArgs e) { try { List<DataRow> drs = e.Data.GetData(typeof(List<DataRow>)) as List<DataRow>; if (drs != null) { FillTreeData(sender, drs); } } catch (Exception ex) { MessageBox.Show("系统提示", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); } } void FillTreeData(object sender, List<DataRow> drs) { foreach (DataRow dr in drs) { this.gridView2.AddNewRow(); gridView2.SetFocusedRowCellValue("id", Guid.NewGuid().ToString()); gridView2.SetFocusedRowCellValue("name", dr["name"].ToString()); gridView2.SetFocusedRowCellValue("code", dr["code"].ToString()); } } #endregion #region 拖出 private void gridView1_MouseDown(object sender, MouseEventArgs e) { GridView view = sender as GridView; GriddownHitInfo = null; GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y)); if (Control.ModifierKeys != Keys.None) return; if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0) { GriddownHitInfo = hitInfo; } } private void gridView1_MouseMove(object sender, MouseEventArgs e) { GridView view = sender as GridView; if (e.Button == MouseButtons.Left && GriddownHitInfo != null) { Size dragSize = SystemInformation.DragSize; Rectangle dragRect = new Rectangle(new Point(GriddownHitInfo.HitPoint.X - dragSize.Width / 2, GriddownHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize); if (!dragRect.Contains(new Point(e.X, e.Y))) { int[] rowint = view.GetSelectedRows(); List<DataRow> row = new List<DataRow>(); foreach (int i in rowint) { row.Add(view.GetDataRow(i)); } if (row != null && row.Count > 0) { view.GridControl.DoDragDrop(row, DragDropEffects.Move); GriddownHitInfo = null; DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true; } } } } #endregion #endregion