TreeList拖放数据到GridControl
拖放操作可以简化界面设计,提高工作效率。操作效果如下:
TreeList拖放数据到GridControl的步骤如下:
(1)设TreeList和GridControl的AllowDrop为ture,默认是不允许拖放的;
(2)定义数据实体,初始化并绑定数据;
(3)在TreeList按下时,获取其位置信息,主要是为了对击中的条目进行判断其是否允许被拖放(本例中未详细判断);
(4)在TreeList移动时,获取移动的条目的信息,这里可以是传递任意对象;
(5)在GridControl进入时,获取其位置信息,主要是为了对击中的条目进行判断其是否允许拖放(本例中未详细判断);
(6)在GridControl松开时,将拖放传递的对象绑定到GridControl数据源中。
代码如下:
using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraTreeList; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace DragDropSample { public partial class Form1 : XtraForm { /// <summary> /// TreeList点击信息 /// </summary> private TreeListHitInfo _treeListHitInfo; /// <summary> /// Grid点击信息 /// </summary> private GridHitInfo _gridHitInfo; public Form1() { InitializeComponent(); //设置允许拖放 this.treeList1.AllowDrop = true; this.gridControl1.AllowDrop = true; //绑定数据 this.treeList1.ParentFieldName = "Pid"; this.treeList1.KeyFieldName = "Id"; this.treeList1.DataSource = GetTreeData(); this.gridControl1.DataSource = null; } // 第一步,treeList按下,获取点击信息 private void treeList1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && Control.ModifierKeys == Keys.None) { _treeListHitInfo = (sender as TreeList)?.CalcHitInfo(e.Location); } else { _treeListHitInfo = null; } } //第二步,treeList移动,设置拖放数据和效果 private void treeList1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left && _treeListHitInfo != null && _treeListHitInfo.HitInfoType == HitInfoType.Cell) { Size size = SystemInformation.DragSize; Rectangle dragRect = new Rectangle(new Point(_treeListHitInfo.MousePoint.X - size.Width / 2, _treeListHitInfo.MousePoint.Y - size.Height / 2), size); if (dragRect.Contains(new Point(e.X, e.Y))) return; //拖放传递的数据可以是任意对象 NodeRow nodeRow = (NodeRow)(sender as TreeList)?.GetRow(_treeListHitInfo.Node.Id); treeList1.DoDragDrop(nodeRow, DragDropEffects.Copy); DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = false; } } //第三步,进入gridControl,获取位置,设置效果 private void gridControl1_DragOver(object sender, DragEventArgs e) { _gridHitInfo = this.gridView1.CalcHitInfo(gridControl1.PointToClient(new Point(e.X, e.Y))); e.Effect = DragDropEffects.Copy; } //第四步,松开鼠标,绑定数据 private void gridControl1_DragDrop(object sender, DragEventArgs e) { string type = string.Empty; object obj = e.Data.GetData(typeof(NodeRow)); NodeRow nodeRow = (NodeRow)obj; List<NodeRow> list = (List<NodeRow>)this.gridControl1.DataSource ?? new List<NodeRow>(); list.Add(nodeRow); this.gridControl1.DataSource = list; this.gridControl1.RefreshDataSource(); } /// <summary> /// 初始化数据 /// </summary> /// <returns></returns> private List<NodeRow> GetTreeData() { NodeRow nodeRow1 = new NodeRow() { Pid = "-1", Id = "1", Name = "四川" }; NodeRow nodeRow2 = new NodeRow() { Pid = "1", Id = "2", Name = "成都" }; NodeRow nodeRow3 = new NodeRow() { Pid = "1", Id = "3", Name = "绵阳" }; NodeRow nodeRow4 = new NodeRow() { Pid = "-1", Id = "4", Name = "湖北" }; NodeRow nodeRow5 = new NodeRow() { Pid = "4", Id = "5", Name = "武汉" }; var nodeRows = new List<NodeRow>() { nodeRow1, nodeRow2, nodeRow3, nodeRow4, nodeRow5 }; return nodeRows; } } /// <summary> /// 自定义对象 /// </summary> public class NodeRow { public string Pid { get; set; } public string Id { get; set; } public string Name { get; set; } } }
作者:我也是个傻瓜
出处:http://www.cnblogs.com/liweis/
签名:成熟是一种明亮而不刺眼的光辉。