Dev GridView行拖拽

來源:互聯網
上載者:User

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;

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;
        }

        //滑鼠按下事件
        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();
            }
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.