首页 > 开发 > 综合 > 正文

C#:从DataGridView控件托放数据到TreeView控件

2024-07-21 02:28:49
字体:
来源:转载
供稿:网友
实现方法,在datagridview的mousedown事件中开始 托放。
然后在treeview 的 dragenter 中接收托放。
最后在treeview的 dragdrop 中处理托放结果。
注:treeview的allowdrop属性要设置为 true。

 1private void datagridview1_mousedown(object sender, mouseeventargs e)
 2        {
 3            if (e.button == mousebuttons.right)
 4            {
 5                datagridview.hittestinfo info = datagridview1.hittest(e.x, e.y);
 6               
 7                if (info.rowindex >= 0)
 8                {
 9                    datagridviewrow dr = (datagridviewrow)
10                           datagridview1.rows[info.rowindex];
11                    if (dr != null)
12                        datagridview1.dodragdrop(dr, dragdropeffects.copy);
13                }
14            }
15        }
16
17        private void treeview1_dragenter(object sender, drageventargs e)
18        {
19            e.effect = dragdropeffects.copy;
20        }
21
22        private void treeview1_dragdrop(object sender, drageventargs e)
23        {
24            if (e.data.getdatapresent(typeof(datagridviewrow)))
25            {               
26                point p = treeview1.pointtoclient(new point(e.x, e.y));
27                treeviewhittestinfo index = treeview1.hittest(p);
28
29                if (index.node != null)
30                {
31
32                    datagridviewrow drv = (datagridviewrow)e.data.getdata(typeof(datagridviewrow));
33                    index.node.text = "drop: " + drv.cells[0].tostring();
34            
35                }
36            }
37        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表