DataGridView拖动的前提:


this.dgv.AllowDrop = true;
this.dgv.MultiSelect = false;
this.dgv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;


拖动需要几个事件支持:

DragDrop:拖动事件

DragEnter:控制拖动范围,影响鼠标状态

CellMouseMove:鼠标移动触发拖动事件,个人觉着可以用其他事件替

private void dgv_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
    if ((e.Clicks < 2) && (e.Button == MouseButtons.Left))
    {
        if (e.RowIndex > -1) //此处可以添加其他限制条件,例如只有在那一列时才允许拖动等
            dgv.DoDragDrop(dgv.Rows[e.RowIndex], DragDropEffects.Move);
    }
}
private void dgv_DragDrop(object sender, DragEventArgs e)
{
     
    int idx = GetRowFromPoint(e.X, e.Y);
    if (idx < 0 || idx == dgv.Rows.Count-1/*这个判断是在允许用户添加行时,排除拖动到最后的空白行处的*/) return;
    if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
    {
        DataGridViewRow row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
        dgv.Rows.Remove(row);
        dgvr.Rows.Insert(idx, row);
        dgv.CurrentCell = dgvBreakLayer[0, idx];
     }
}
private int GetRowFromPoint(int x, int y)
{
    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 dgv_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}


后期要是发现更好的方法会持续改进,欢迎大家提出宝贵意见!