一佳一

记录像1+1一样简洁的代码

导航

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

Posted on 2010-12-21 11:30  一佳一  阅读(6505)  评论(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