在dragdrop中的代码如下:
DragDropTargetIndex = this.HitTest(ClientPoint.X, ClientPoint.Y).RowIndex;
 if (DragDropTargetIndex > -1 && DragDropCurrentIndex < this.RowCount ) 
{
 DragDropCurrentIndex = -1; 
DataGridViewRow SourceRow = drgevent.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
 this.Rows.RemoveAt(DragDropSourceIndex);
 this.Rows.Insert(DragDropTargetIndex, SourceRow); 
this.Rows[DragDropTargetIndex].Selected = true;
 this.CurrentCell = this[0, DragDropTargetIndex];
}
拖动的行的删除了,但是在新位置却没有插入新行,如果换成datatable,
DataGridViewRow  r = (DataGridViewRow )e.Data.GetData(typeof(DataGridViewRow ));                    DataTable tmpdt = (DataTable)dataGridView1.DataSource;                    DataRow r1 = tmpdt.NewRow ();                    for (int i = 0; i < tmpdt.Columns.Count; i++)                    {                        r1[i] = r.Cells[i].Value;                    }                    tmpdt.Rows.RemoveAt(r.Index );                    tmpdt.Rows.InsertAt(r1, Index);
倒是可以插入新行了,但是如果一拖拖动列的话,再拖动行的话又不对了,

解决方案 »

  1.   

    需要处理 DataTable , 不用处理DataGridView移动的方法可以优化:
    object[] temp = dt.Rows[r.Index].ItemArray;  // 1.要移动的数据行存入临时变量
    dt.Rows[r1].ItemArray = temp;                // 2.把临时变量中的数据存入当前行直接操作DataTable,拖动列,对拖动行没有影响。
      

  2.   

    这样移动是移动了,但是把原先行上的数据覆盖了,能不覆盖不,谢谢lzsh0622
      

  3.   

    而且如果先拖动了一列的话,拖动的那列的值也没有跟着在拖动行时移动,问一下一个很弱的问题,datagridview如何插入空行,用数据源如何插入,不用数据源如何直接插入,谢谢
      

  4.   

    DataGridViewRow row1 = new DataGridViewRow();
                                DataGridViewTextBoxCell[] cells = new DataGridViewTextBoxCell[this.ColumnCount ];
                                for(int i=0;i<this.ColumnCount;i++)
                                {
                                    cells[i].Value=SourceRow.Cells[i].Value;
                                    row1.Cells.Add(cells[i]);
                                }
    我单步调试的时候,代码运行到cells[i].Value=SourceRow.Cells[i].Value;
    不向下运行了,是那里的问题,急
      

  5.   

    datagridview绑定数据源后,不能insert