一佳一

记录像1+1一样简洁的代码
随笔 - 396, 文章 - 0, 评论 - 95, 阅读 - 107万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

C# Devexpress 中GridControl多行数据拖拽(转)

Posted on   一佳一  阅读(6526)  评论(0编辑  收藏  举报

using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid;

// 声明:

 private GridHitInfo downHitInfo;

设置多行选中

 GridView1.OptionsSelection.MultiSelect = true;

//事件处理:

//要拖动的数据

  private void GridControl1_MouseDown(object sender, MouseEventArgs e)
        {
            downHitInfo = GridView1.CalcHitInfo(new Point(e.X, e.Y));
        }

        private void GridControl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (downHitInfo == null) return;
            if (e.Button != MouseButtons.Left) return;
            Rectangle dragRect = new Rectangle(new Point(
                downHitInfo.HitPoint.X - SystemInformation.DragSize.Width / 2,
                downHitInfo.HitPoint.Y - SystemInformation.DragSize.Height / 2), SystemInformation.DragSize);
            if (!dragRect.Contains(new Point(e.X, e.Y)))
            {
                DataRow data = GridView1.GetDataRow(downHitInfo.RowHandle);
                int[] rows = GridView1.GetSelectedRows();
                List<DataRow> row = new List<DataRow>();
                for (int i = 0; i < rows.Length; i++)
                    row.Add(GridView1.GetDataRow((int)rows[i]));
                GridControl1.DoDragDrop(row, DragDropEffects.Copy);
            }
        }

//要存放的GridControl

// 设置为可接受拖拽数据 

 GridControl2.AllowDrop = true;

  private void GridControl2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void GridControl2_DragDrop(object sender, DragEventArgs e)
        {
            GridControl grid = sender as GridControl;
            DataTable table = grid.DataSource as DataTable;
            List<DataRow> row = e.Data.GetData(typeof(List<DataRow>)) as List<DataRow>;
            if (row != null && table != null)
            {
                if (row.Count > 0)
                {
                    for (int i = 0; i < row.Count; i++)
                    {
                        table.ImportRow(row[i]);
                        row[i].Delete();  // 把原有的数据行删除。
                    }
                }
            }
        }


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Fing_king/archive/2010/08/01/5781591.aspx

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示