Dev GridView行拖拽

http://blog.csdn.net/keyrainie/article/details/8513802
http://www.cnblogs.com/qq4004229/archive/2012/05/28/2521611.html
http://www.cnblogs.com/zeroone/p/4176887.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;html

namespace DevGridViewDragDropTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SetDtSource();
GridViewInit();
BindGrid();
}
//鼠標座標
private GridHitInfo downHitInfo; //鼠標左鍵按下去時在GridView中的座標
private GridHitInfo upHitInfo; //鼠標左鍵彈起來時在GridView中的座標
private int startRow; // 拖拽的第一行
private int[] rows; //拖拽的全部行
//數據源
DataTable dtSource;
private void SetDtSource()
{
dtSource = new DataTable();
dtSource.Columns.Add("id", typeof(int));
dtSource.Columns.Add("name", typeof(string));
dtSource.Columns.Add("age", typeof(int));
dtSource.Columns.Add("add", typeof(string));
DataRow dr;
for (int i = 0; i < 20; i++)
{
dr = dtSource.NewRow();
dr["id"] = i;
if (i % 2 == 1)
{
dr["name"] = "小張" + i;
dr["age"] = 20 + i;
}
else
{
dr["name"] = "老李" + i;
dr["age"] = 10 + i;
}
dr["add"] = "陝西省西安市第" + i + "大道";
dtSource.Rows.Add(dr);
}
}
private void GridViewInit()
{
gridControl1.AllowDrop = true; // 確保可以拖拽
gridView1.OptionsSelection.MultiSelect = true; //確保可以多選
gridView1.OptionsSelection.EnableAppearanceFocusedCell = false; //確保選定行的背景色同樣
gridView1.OptionsSelection.EnableAppearanceFocusedCell = false; //確保選定行的背景色同樣。
gridView1.OptionsBehavior.Editable = false;
}
private void BindGrid()
{
gridControl1.DataSource = dtSource;
}this

//鼠標按下事件
    private void gridView1_MouseDown(object sender, MouseEventArgs e)
    {
        downHitInfo = gridView1.CalcHitInfo(new Point(e.X, e.Y));   //鼠標左鍵按下去時在GridView中的座標
    }

    //鼠標移動事件
    private void gridView1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;        //不是左鍵則無效
        if (downHitInfo == null || downHitInfo.RowHandle < 0) return;   //判斷鼠標的位置是否有效

        rows = gridView1.GetSelectedRows();  //獲取所選行的index
        startRow = rows.Length == 0 ? -1 : rows[0];
        DataTable dt = dtSource.Clone();

        foreach (int r in rows)   // 根據所選行的index進行取值,去除所選的行數據,多是選取的多行
        {
            int dataSourcerows = gridView1.GetDataSourceRowIndex(r); //獲取行數據
            dt.ImportRow(dtSource.Rows[dataSourcerows]); //保存所選取的行數據
        }
       gridControl1.DoDragDrop(dt, DragDropEffects.Move);//開始拖放操做,將拖拽的數據存儲起來
    }

    //拖拽過程事件
    private void gridControl1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    //拖拽完成後事件
    private void gridControl1_DragDrop(object sender, DragEventArgs e)
    {
        Point gridviewPoint = this.PointToScreen(this.gridControl1.Location);  //獲取鼠標在屏幕上的位置。
        upHitInfo = gridView1.CalcHitInfo(new Point(e.X - gridviewPoint.X, e.Y - gridviewPoint.Y));   //鼠標左鍵彈起來時在GridView中的座標(屏幕位置減去 gridView 開始位置)
        if (upHitInfo == null || upHitInfo.RowHandle < 0) return;

        int endRow = gridView1.GetDataSourceRowIndex(gridView1.GetDataSourceRowIndex(upHitInfo.RowHandle)); //獲取拖拽的目標行index

        DataTable dt = e.Data.GetData(typeof(DataTable)) as DataTable;  //獲取要移動的數據,從拖拽保存的地方:(gridControl1.DoDragDrop(dt, DragDropEffects.Move); )

        if (dt != null && dt.Rows.Count > 0) //拖拽的數據爲空
        {
            int a;
            DataRow xs = dtSource.Rows[endRow]; //獲取拖拽的目標行,準備進行移植
            if (!rows.Contains(endRow))  //若是多選的話,確保不能拖拽到這幾個裏
            {
                gridView1.ClearSelection();  //從GirdView中刪除所拖拽的數據
                int moveValue = 0;
                foreach (int i in rows)
                {
                    dtSource.Rows.Remove(dtSource.Rows[i - moveValue]);  //從GirdView的數據源中刪除所拖拽的數據
                    moveValue++;
                }

                if (startRow > endRow)
                    a = dtSource.Rows.IndexOf(xs);  //若果往上託,則加在鼠標到達行的上面
                else
                    a = dtSource.Rows.IndexOf(xs) + 1;  //若是往下拖,則加在鼠標到達行的下面
                int j = 0;
                DataRow drTemp;
                foreach (DataRow dr in dt.Rows)
                {
                    drTemp = dtSource.NewRow();
                    foreach (DataColumn dc in dr.Table.Columns)
                    {
                        drTemp[dc.ColumnName] = dr[dc.ColumnName];
                    }
                    dtSource.Rows.InsertAt(drTemp, a + j); //將拖拽的數據再次添加進來
                    gridView1.SelectRow(a + j);
                    j++;
                }
                gridView1.FocusedRowHandle = a;
            }
            gridControl1.DataSource = dtSource; //從新綁定
            gridView1.RefreshData();
        }
    }
}

}spa

相關文章
相關標籤/搜索