dev gridview 单元格值拖拽替换

public class GridViewDropCell
    {
        //dvginfo根据鼠标点击的x、y坐标获取该点的相关信息
        private GridHitInfo downHitInfo;
        private GridHitInfo upHitInfo;

        private GridView _dgv;//表格
        private GridControl _dgvinfo;
        public GridViewDropCell(GridView dgv, GridControl dgvinfo)
        {
            _dgv = dgv;
            _dgvinfo = dgvinfo;
            _dgvinfo.AllowDrop = true; // 确保能够拖拽
        }

        public void MouseDown(MouseEventArgs e)
        {
            downHitInfo = _dgv.CalcHitInfo(new Point(e.X, e.Y));
        }

        public void MouseMove(MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;        //判断是否是左键
            if (downHitInfo == null || downHitInfo.RowHandle < 0) return;   //判断按下的位置是否有值,是否是gridview 的Items. 
            GridCell[] gcs = _dgv.GetSelectedCells();
            _dgvinfo.DoDragDrop(gcs, DragDropEffects.Move);//开始拖放操作。
        }
        //拖拽过程事件     
        public void DragOver(DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }
        //拖拽完成后事件  
        public void DragDrop(DragEventArgs e, Point gridviewPoint)
        {
            //获取鼠标在屏幕上的位置。
            //Point gridviewPoint = this.PointToScreen(this._dgvinfo.Location);
            //获取 gridview 中对应的的位置(屏幕位置减去 gridView 开始位置)
            upHitInfo = _dgv.CalcHitInfo(new Point(e.X - gridviewPoint.X, e.Y - gridviewPoint.Y));
            if (upHitInfo == null || upHitInfo.RowHandle < 0) return;
            //获取释放的位置列索引
            int endRow = upHitInfo.RowHandle;
            GridColumn column = upHitInfo.Column;//所在单元格
            GridCell[] cells = e.Data.GetData(typeof(GridCell[])) as GridCell[];//获取要移动的数据
            if (cells != null && cells.Count() > 0)
            {
                _dgv.ClearSelection();
                if (cells.Count() == 1)//只选择一条数据
                {
                    ReplaceACellValue(cells.FirstOrDefault(), endRow, column);
                }
                else if (cells.Where(item => item.Column.Equals(column)).Count() == 0)//选择的都是同一列数据
                {
                    //选择一列当前索引小于选择的索引的数据有多少
                    int cellRow = cells.Where(item => item.RowHandle < endRow).Count();
                    int endcell = endRow - cellRow;
                    foreach (GridCell fc in cells)
                    {
                        ReplaceACellValue(fc, endcell, column);
                        endcell++;
                    }
                }
                else
                { 
//两列数据,
//当前列选择的在当前索引上的数据有多少 int cellRow = cells.Where(item => item.Column.Equals(column) && item.RowHandle < endRow).Count(); int endcell = endRow - cellRow; //另一列选择的在当前索引上的数据有多少 int cell2Row = cells.Where(item => item.RowHandle < endRow).Count() - cellRow; int end2cell = endRow - cellRow; foreach (GridCell fc in cells) { if (fc.Column.Equals(column)) { ReplaceCellValue(fc, endcell); endcell++; } else { ReplaceCellValue(fc, end2cell); end2cell++; } } } } _dgv.RefreshData(); } /// <summary> /// 单个单元格替换 /// </summary> /// <param name="cell">选择的单元格</param> /// <param name="endRow">替换的单元格索引</param> /// <param name="column">替换的单元格列</param> private void ReplaceACellValue(GridCell fc, int endRow, GridColumn column) { object cellvalue = _dgv.GetRowCellValue(fc.RowHandle, fc.Column); object value = _dgv.GetRowCellValue(endRow, column); _dgv.SetRowCellValue(endRow, column, cellvalue);//选择的数据赋值 _dgv.SetRowCellValue(fc.RowHandle, fc.Column, value);//替换数据 _dgv.SelectCell(endRow, column); } /// <summary> /// 替换单元格的值 /// </summary> private void ReplaceCellValue(GridCell fc, int rowindex) { object cellvalue = _dgv.GetRowCellValue(fc.RowHandle, fc.Column); object value = _dgv.GetRowCellValue(rowindex, fc.Column);//当前选中的值 _dgv.SetRowCellValue(rowindex, fc.Column, cellvalue);//选择的数据赋值 _dgv.SetRowCellValue(fc.RowHandle, fc.Column, value);//替换数据 _dgv.SelectCell(rowindex, fc.Column); } }

效果

 

posted @ 2019-04-02 10:11  世人皆萌  阅读(570)  评论(0编辑  收藏  举报