查询了许多资料都是单个单元格的替换,而我这边则需要行的拖拽.,我这边记录数据源的行位置,拖拽后则从源数据读取对应的数据添加到目标源中。

例如要实现从gridSource到gridTarget的拖拽,需要一个设置和三个事件:
1、设置gridTarget的属性AllowDrop为True
2、实现gridSource的MouseDown事件,在这里进行要拖拽的Cell内容的保存,保存到剪贴板。
3、实现gridTarget的DragDrop和DragEnter事件,DragDrop事件中的一个难点就是决定拖拽到哪一个Cell

/// <summary>
        /// 数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
            if (e.Button == MouseButtons.Left)
                DataGridView.HitTestInfo info = this.dataGridView1.HitTest(e.X, e.Y);
                if (info.RowIndex >= 0)
                    if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
                        int currow = info.RowIndex;//记录数据位置,并拷贝
                        if (currow >-1)
                            this.dataGridView1.DoDragDrop(currow, DragDropEffects.Copy);
 /// <summary>
        /// 目标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridView2_DragDrop(object sender, DragEventArgs e)
            if (e.Data != null)
                int rowindexs = (int)e.Data.GetData(typeof(int));//目标源选择的行位置
                DataTable dt = (DataTable)dataGridView1.DataSource;//数据源数据,然后进行对应操作
        private void DataGridView2_DragEnter(object sender, DragEventArgs e)
            e.Effect = DragDropEffects.Copy;

多行数据拖拽

   /// <summary>
        /// 多行数据拖拽(多选拖拽时务必按住ctrl,否则只会拖拽一行)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridViewX1_MouseDown(object sender, MouseEventArgs e)
            if (e.Button == MouseButtons.Left)
                DataGridView.HitTestInfo info = this.dataGridViewX1.HitTest(e.X, e.Y);
                if (info.RowIndex >= 0)
                    if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
                        int currow = info.RowIndex;//记录数据位置,并拷贝
                        DataGridViewSelectedRowCollection cs = dataGridViewX1.SelectedRows;
                        if (currow > -1)
                            this.dataGridViewX1.DoDragDrop(cs, DragDropEffects.Copy);
  /// <summary>
        /// 目标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridView2_DragDrop(object sender, DragEventArgs e)
            if (e.Data != null)
                DataGridViewSelectedRowCollection rowindexs = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));//目标源选择的行位置
                foreach (DataGridViewRow row in rowindexs)//数据获取
                    string value = row.Cells[1].Value.ToString();
                DataTable dt = (DataTable)dataGridView1.DataSource;//数据源数据,然后进行对应操作
                                    今天讲的是如何在dataGridView1中用鼠标选中一行数据,然后将这行数据传到dataGridView2中进行显示。
首先我在winform窗体中拉了三个panel,两个button,两个dataGridView,来实现这个小项目。设计完如下图所示:
程序运行之后实现的效果是如下图所示:
点击向右的箭头就将dataGridView1中的数据转移到dataGridView2中。
点击向左的箭头就将dataGridView2中的数据转移到dataGridView1中。
实现的代码如下所示:
  privat
                                    private void dataGridView_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)         {             if ((e.Clicks             {                 if ((e.ColumnIndex ==-1) && (e.RowIndex > -1)
this.gridControl1.AllowDrop = true; // 确保能够拖拽
this.gridView1.OptionsSelection.MultiSelect = true; //确保能够多选
this.gridView.
                                            //下移一行         private void button2_Click( object sender, EventArgs e )        {            DataGridViewRow dr = dataGridView1.CurrentRow;            if ( dr.Index             
                                    DataGridView拖动的前提:this.dgv.AllowDrop = true;
this.dgv.MultiSelect = false;
this.dgv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;拖动需要几个事件支持:DragDrop:拖动事件Drag...
                                     DataGridView.CurrentRow 属性 :获取包含当前单元格的行。只能为一行。 表示当前单元格所在的行的 DataGridViewRow,如果没有当前单元格,则为 null。例:DataGridView.CurrentRow.Cells[列index].Value取值。int i = DataGridView1.CurrentRow.Index; 选择单元框所在行的