C# Devexpress 中 listBoxControl 数据拖拽

本文介绍了如何在C#中使用DevExpress的listBoxControl实现数据的拖放功能。通过设置AllowDrop属性允许拖放,然后在鼠标移动和按下事件中处理拖放源的操作。在目标listBoxControl的DragDrop和DragEnter事件中处理数据接收和效果显示。拖放的数据类型为List<DataRow>,适用于GridControl等组件。

        this.listBoxControl2.AllowDrop = true;//获取或设置一个值,该值指示控件是否可以接受用户拖放到它上面的数据。

 

        //源ListBoxControl
        private void listBoxControl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if ((p != Point.Empty) && ((Math.Abs(e.X - p.X) > SystemInformation.DragSize.Width) || (Math.Abs(e.Y - p.Y) > SystemInformation.DragSize.Height)))
                {
                    DataRowView row = listBoxControl1.SelectedItem as DataRowView;
                    List<DataRow> rows = new List<DataRow>();
                    rows.Add(row.Row);
                    listBoxControl1.DoDragDrop(rows, DragDropEffects.Copy);

                    // 方法二
                    //listBoxControl1.DoDragDrop(sender, DragDropEffects.Copy);
                }
            }
        }
        //源ListBoxControl
        private void listBoxControl1_MouseDown(object sender, MouseEventArgs e)
        {
            ListBoxControl c = sender as ListBoxControl;
            p = new Point(e.X, e.Y);
            int selectedIndex = c.IndexFromPoint(p);
            if (selectedIndex == -1)
                p = Point.Empty;
        }
        //目标ListBoxControl
        private void listBoxControl2_DragDrop(object sender, DragEventArgs e)
        {
            ListBoxControl listBox = sender as ListBoxControl;
            DataTable table = listBox.DataSource as DataTable;
            List<DataRow> rows = e.Data.GetData(typeof(List<DataRow>)) as List<DataRow>;
            if (rows != null && table != null)
            {
                if (rows.Count > 0)
                {
                    for (int i = 0; i < rows.Count; i++)
                    {

                        //将 System.Data.DataRow 复制到 System.Data.DataTable 中,保留任何属性设置以及初始值和当前值。
                        table.ImportRow(rows[i]);

                        //把源ListBoxControl里面数据行删除
                        rows[i].Delete();
                    }
                }
            }
            //方法二
            //listBoxControl1.Refresh();

            //ListBoxControl listBox = sender as ListBoxControl;
            //Point newPoint = new Point(e.X, e.Y);
            //newPoint = listBox.PointToClient(newPoint);
            //int selectedIndex = listBox.IndexFromPoint(newPoint);
            //object item = listBoxControl1.Items[listBoxControl1.IndexFromPoint(p)];
            //if (selectedIndex == -1)
            //    listBox.Items.Add(item);
            //else
            //    listBox.Items.Insert(selectedIndex, item);

        }
        //目标ListBoxControl
        private void listBoxControl2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

 

拖拽数据为List<DataRow>,GridControl同样可以接收。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值