C#开发学习笔记:C#中实现两个GridControl之间的数据拖拽以及同一个GridControl中的数据行上下移动

1.两个控件之间的拖拽

(1).拖拽发起方控件使用MouseDown事件获取需要拖拽的数据行的索引

DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo GridHitInfo_temp = null;
private void tRC01GridControl_MouseDown(object sender, MouseEventArgs e)
{
 //获取鼠标当前所在坐标点的信息(该点相对于控件的坐标系)
 DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo GridHitInfo = this.gridView3.CalcHitInfo(new Point(e.X, e.Y));
 //获取鼠标当前所在坐标的对应的数据行索引
 int MouseRowHandle = GridHitInfo.RowHandle;
 //如果索引大于0同时鼠标点击的是左键
 if (MouseRowHandle >= 0 && e.Button == MouseButtons.Left)
 {
     GridHitInfo_temp = GridHitInfo;
 }
}

注:如果需要拖拽多行,则可以在此步获取鼠标选中范围时点击的起始数据行和结束数据行的索引

(2).拖拽发起方控件使用MouseMove事件获取需要拖拽的数据行

private void tRC01GridControl_MouseMove(object sender, MouseEventArgs e)
{
	if (GridHitInfo_temp == null) return;
      if (e.Button != MouseButtons.Left) return;
     //通过鼠标按下时获取的数据行索引获取数据
     DataRow data = gridView3.GetDataRow(GridHitInfo_temp.RowHandle);
     int[] rows = gridView3.GetSelectedRows();//获取当前选中的数据行的索引
     //定义一个泛型集合存储拖拽的数据
     List<DataRow> row = new List<DataRow>();
     for (int i = 0; i < rows.Length; i++)
         row.Add(gridView3.GetDataRow((int)rows[i]));


     //拖拽发起方控件开始执行拖拽操作,拖拽操作的类型为Copy(复制)
     tRC01GridControl.DoDragDrop(row, DragDropEffects.Copy);
}
注:如果是拖拽多行,则使用SelectRange(int startRowHandle,int endRowHandle);其中两个参数则使用第一步获取到的起始与结束行索引


(3).设置目的控件的拖拽操作方式

private void tRC02GridControl_DragEnter(object sender, DragEventArgs e)
{
     //根据拖拽事件发起方所允许的拖放操作设置目的方的拖拽操作类型
      if (e.AllowedEffect==DragDropEffects.Move)
         e.Effect = DragDropEffects.Move;
      else
         e.Effect = DragDropEffects.Copy;
}


(4).拖拽完成,目的控件执行DragDrop事件

 private void tRC02GridControl_DragDrop(object sender, DragEventArgs e)
{
    GridControl grid = sender as GridControl;//将拖拽事件的触发对象(目的方)转为控件
    DataTable table = grid.DataSource as DataTable;//获取拖拽目的方的数据源

    //获取拖拽的源数据(在调用DoDragDrop()方法时传入)同时转换为传入的类型
    List<DataRow> row = e.Data.GetData(typeof(List<DataRow>)) as List<DataRow>;
    if (row != null && table != null)
    {
        if (row.Count > 0)
        {
            //判断当前拖拽操作的方式
            if (e.Effect != DragDropEffects.Move)
            {
                for (int i = 0; i < row.Count; i++)
                {
                    row[i]["Selection"] = "True";
                    table.ImportRow(row[i]);
                    row[i].Delete();  // 把原有的数据行删除(会在数据行的父表中删除)。
                }
            }
         }
    }
}


2.一个GridControl中的数据行上下移动

(1),(2),(3)步同上,只是调用对应的事件的控件为当前的控件

(4).执行DragDrop事件

 private void tRC02GridControl_DragDrop(object sender, DragEventArgs e)
{
    GridControl grid = sender as GridControl;//将拖拽事件的触发对象(目的方)转为控件
    //获取目的方控件的默认GridView
    DevExpress.XtraGrid.Views.Grid.GridView gridView = grid.DefaultView as DevExpress.XtraGrid.Views.Grid.GridView;
    DataTable table = grid.DataSource as DataTable;//获取拖拽目的方的数据源

    //根据鼠标在目的方的坐标获取对应的信息
    //该坐标是根据鼠标相对于的屏幕的位置(Control.MousePosition)使用控件的PointToClient()方法转换;
    //如果需要调试需要注意Control.MousePosition的坐标在调试状态下也会更改,因此调试时需要将断点设置在Control.MousePosition
    //转换为Point对象之后才能保证转换出来的鼠标相对于控件工作区域的坐标正确
    DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo GridHitInfo = gridView.CalcHitInfo(grid.PointToClient(Control.MousePosition));

    //获取拖拽的源数据(在调用DoDragDrop()方法时传入)同时转换为传入的类型
    List<DataRow> row = e.Data.GetData(typeof(List<DataRow>)) as List<DataRow>;
    if (row != null && table != null)
    {
        if (row.Count > 0)
        {
            //判断当前拖拽操作的方式
            if (e.Effect == DragDropEffects.Move)
            {
                for (int i = 0; i < row.Count; i++)
                {
                    if (GridHitInfo.RowHandle >= 0)
                    {
                        /根据鼠标转换坐标之后计算出来其相对目的控件的坐标点所在行的索引新增一条空行
                        table.Rows.InsertAt(table.NewRow(), GridHitInfo.RowHandle);
                        //将拖拽的数据行的信息赋值到新增的空行中
                        table.Rows[GridHitInfo.RowHandle].ItemArray = row[i].ItemArray;

                    }
                    else
                    {
                        table.Rows.Add();
                        table.Rows[table.Rows.Count-1].ItemArray = row[i].ItemArray;
                    }

                    row[i].Delete();//删除原来的数据行(该行所在父表中删除)
                    table.AcceptChanges();//提交表所有的更改
                }
            }
        }
    }
}

注:使用Control.MousePosition获取鼠标标相对于屏幕的坐标时,在调试状态下Control.MousePosition也会随着鼠标的移动而改变,因此调试时

     为了使鼠标的屏幕坐标使用控件的PointToClient()方法转换为其相对于控件的坐标时正确,需要注意断点要打在转换之后


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值