在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);
倒是可以插入新行了,但是如果一拖拖动列的话,再拖动行的话又不对了,
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);
倒是可以插入新行了,但是如果一拖拖动列的话,再拖动行的话又不对了,
object[] temp = dt.Rows[r.Index].ItemArray; // 1.要移动的数据行存入临时变量
dt.Rows[r1].ItemArray = temp; // 2.把临时变量中的数据存入当前行直接操作DataTable,拖动列,对拖动行没有影响。
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;
不向下运行了,是那里的问题,急