两个表结构相同的datagridview间的整行拖动(转载)

转自http://hi.baidu.com/zouqiner/blog/item/3890f363d0e37ad5e6113a56.html

    注意里面所有的事件都是两个datagridview都需要挂上~才能实现拖动,转载请注明

      欢迎更多热爱.net 的朋友一起探讨研究。

界面样式:

源码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private int selectionIdx = 0;

        private void Form3_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("table1");
            ds.Tables[0].Columns.Add("code");
            ds.Tables[0].Columns.Add("desc");
            ds.Tables[0].Rows.Add(new object[] { "1", "aaa" });
            ds.Tables[0].Rows.Add(new object[] { "2", "bbb" });

            DataSet ds2 = new DataSet();
            ds2.Tables.Add("table1");
            ds2.Tables[0].Columns.Add("code");
            ds2.Tables[0].Columns.Add("desc");
            ds2.Tables[0].Rows.Add(new object[] { "3", "ccc" });
            ds2.Tables[0].Rows.Add(new object[] { "4", "ddd" });

            dataGridView1.DataSource = ds.Tables[0];
            dataGridView2.DataSource = ds2.Tables[0];
        }

        DataRow dr = null;
        DataGridViewRow dgvr = null;

        private void dataGridView1_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                if (dr != null)
                {
                    DataGridView dgv = sender as DataGridView;
                    string strTemp = dgv.Name;
                    int idx = GetRowFromPoint(e.X, e.Y, dgv);
                    if (idx < 0)
                        idx = dgv.Rows.Count;
                    if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
                    {
                        ((DataTable)dgv.DataSource).Rows.InsertAt(dr, idx);
                        selectionIdx = idx;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void dataGridView1_DragOver(object sender, DragEventArgs e)
        {
            try
            {
                DataGridView dgv = sender as DataGridView;
                string strTemp = dgv.Name;

                dgvr = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));

                dr = ((DataTable)dgv.DataSource).NewRow();
                for (int i = 0; i < dgvr.Cells.Count; i++)
                {
                    dr[i] = dgvr.Cells[i].Value;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {

                if (e.RowIndex > 0)
                    selectionIdx = e.RowIndex;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private int GetRowFromPoint(int x, int y,DataGridView dgv)
        {

            for (int i = 0; i < dgv.RowCount; i++)
            {
                Rectangle rec = dgv.GetRowDisplayRectangle(i, false);

                if (dgv.RectangleToScreen(rec).Contains(x, y))
                    return i;
            }

            return -1;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                DataGridView dgv = sender as DataGridView;
                if ((dgv.Rows.Count > 0) && (dgv.SelectedRows.Count > 0))
                {

                    if (dgv.Rows.Count <= selectionIdx)
                        selectionIdx = dgv.Rows.Count - 1;
                    dgv.Rows[selectionIdx].Selected = true;
                    dgv.CurrentCell = dgv.Rows[selectionIdx].Cells[0];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                DataGridView dgv = sender as DataGridView;
                string strTemp = dgv.Name;
                if ((e.Clicks < 2) && (e.Button == MouseButtons.Left))
                {
                    if ((e.ColumnIndex == -1) && (e.RowIndex > -1))
                    {

                        DragDropEffects dd = dgv.DoDragDrop(dgv.Rows[e.RowIndex], DragDropEffects.Move);

                        if (dd == DragDropEffects.Move && dgvr != null)
                        {
                            dgv.Rows.Remove(dgvr);
                            dgvr = null;
                            dr = null;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}

posted @ 2010-01-05 14:35  溪水云天  阅读(784)  评论(0编辑  收藏  举报