datagridview 上移 下移一行交换数据(Move DataGridView up and down one line to exchange data)

交换数据不闪烁 ,复制数据 删除选中的行插入数据会有明显的闪烁

int rowIndex = dgv1.SelectedRows[0].Index; /*得到当前选中行的索引*/rowIndex = 0; /*表示当前行是第1行*/rowIndex = dgv1.Rows.Count – 1; /*当前行是最后一行*/rowIndex =rowIndex – 1; /*当前行是上一行*/rowIndex =rowIndex + 1; /*当前行是下一行*/

List<object> list = new List<object>(); /*使用object List对象因为 列的数据是不确定的*/

/*将选中行的所有单元格添加到List对象中 单元格的总数就是列的总数*/for (int i = 0; i < dgv1.Columns.Count; i++) list.Add(dgv1.SelectedRows[0].Cells[i].Value);

dgv1.Rows[rowIndex].Cells[i].Value /*这是选中的行*/dgv1.Rows[rowIndex – 1].Cells[i].Value; /*……上一行*/

list[i] /*这里放的之前选中的行数据*/

dgv1.Rows[rowIndex].Selected = false; /*设置或取消选中的行 true|false*/

   #region datagridview 上移 下移交换数据

    /// <summary>
    /// 选中的项目上移一个
    /// </summary>
    /// <param name="dgv1"></param>
    public static void MoveItemUp_DataGridView(DataGridView dgv1) {
        int rowIndex = dgv1.SelectedRows[0].Index;  /*得到当前选中行的索引*/

        if (rowIndex == 0)
        {
            MessageBox.Show("已经是第一行了!");
            return;
        }

        List<object> list = new List<object>();
        for (int i = 0; i < dgv1.Columns.Count; i++)
            list.Add(dgv1.SelectedRows[0].Cells[i].Value);   


        for (int i = 0; i < dgv1.Columns.Count; i++)
        {
            dgv1.Rows[rowIndex].Cells[i].Value = dgv1.Rows[rowIndex - 1].Cells[i].Value;/*选中的行*/
            dgv1.Rows[rowIndex - 1].Cells[i].Value = list[i];  /*上一行*/
        }
        dgv1.Rows[rowIndex - 1].Selected = true;
        dgv1.Rows[rowIndex].Selected = false;
    }

    /// <summary>
    /// 选中的项目下移一个
    /// </summary>
    public static void MoveItemDown_DataGridView(DataGridView dgv1) {
        int rowIndex = dgv1.SelectedRows[0].Index;  

        if (rowIndex == dgv1.Rows.Count - 1)
        {
            MessageBox.Show("已经是最后一行了!");
            return;
        }

        List<object> list = new List<object>();/*把当前选中行的数据存入list数组中*/
        for (int i = 0; i < dgv1.Columns.Count; i++)
            list.Add(dgv1.SelectedRows[0].Cells[i].Value);   

        for (int i = 0; i < dgv1.Columns.Count; i++)
        {
            /*选中的行                              下一行  */
            dgv1.Rows[rowIndex].Cells[i].Value = dgv1.Rows[rowIndex + 1].Cells[i].Value;
            dgv1.Rows[rowIndex + 1].Cells[i].Value = list[i];
        }
        dgv1.Rows[rowIndex + 1].Selected = true;
        dgv1.Rows[rowIndex].Selected = false;
    }

    /// <summary>
    /// 移到最顶部
    /// </summary>
    public static void MoveItemTop_DataGridView(DataGridView dgv1) {
        int rowIndex = dgv1.SelectedRows[0].Index;

        if (rowIndex == 0)
        {
            MessageBox.Show("已经是第一行了!");
            return;
        }

        List<object> list = new List<object>();
        for (int i = 0; i < dgv1.Columns.Count; i++)
            list.Add(dgv1.SelectedRows[0].Cells[i].Value); 

        /*选中的行和第一行交换*/
        for (int i = 0; i < dgv1.Columns.Count; i++)
        {
            dgv1.Rows[rowIndex].Cells[i].Value = dgv1.Rows[0].Cells[i].Value; /*选中的行*/
            dgv1.Rows[0].Cells[i].Value = list[i];   /*第一行*/
        }
        dgv1.Rows[0].Selected = true;
        dgv1.Rows[rowIndex].Selected = false;
    }

    /// <summary>
    /// 移动到最底端
    /// </summary>
    public static void MoveItemBottom_DataGridView(DataGridView dgv1) {
        int rowIndex = dgv1.SelectedRows[0].Index;  

        if (rowIndex == dgv1.Rows.Count - 1)
        {
            MessageBox.Show("已经是最后一行了!");
            return;
        }

        List<object> list = new List<object>();
        for (int i = 0; i < dgv1.Columns.Count; i++)
            list.Add(dgv1.SelectedRows[0].Cells[i].Value); 

        
        for (int i = 0; i < dgv1.Columns.Count; i++)
        {
            /*选中的行                              最后一行         交换*/
            dgv1.Rows[rowIndex].Cells[i].Value = dgv1.Rows[dgv1.Rows.Count - 1].Cells[i].Value; 
            dgv1.Rows[dgv1.Rows.Count - 1].Cells[i].Value = list[i];   /*最后一行 =  选中的行*/
        }
        dgv1.Rows[dgv1.Rows.Count - 1].Selected = true;
        dgv1.Rows[rowIndex].Selected = false;
    }



    #endregion
————————

The exchange data does not flash, the copy data deletes the selected row, and the inserted data will flash obviously

int rowIndex = dgv1.SelectedRows[0].Index; /* Get the index of the currently selected row * / rowindex = 0; /* Indicates that the current row is row 1 * / rowindex = dgv1.rows.count – 1/* The current line is the last line * / rowindex = rowindex – 1; /* The current line is the previous line * / rowindex = rowindex + 1; /* The current line is the next line*/

List< object> list = new List< object> (); /* Use the object list object because the data of the column is uncertain*/

/*Add all cells of the selected row to the list object. The total number of cells is the total number of columns * / for (int i = 0; I & lt; dgv1. Columns. Count; I + +) list.Add(dgv1.SelectedRows[0].Cells[i].Value);

dgv1.Rows[rowIndex].Cells[i].Value /* This is the selected row * / dgv1. Rows [rowindex – 1]. Cells [i]. Value; /*…… Previous line*/

List [i] / * the previously selected row data is placed here*/

dgv1.Rows[rowIndex].Selected = false; /* Set or uncheck rows true | false*/

   #region datagridview 上移 下移交换数据

    /// <summary>
    /// 选中的项目上移一个
    /// </summary>
    /// <param name="dgv1"></param>
    public static void MoveItemUp_DataGridView(DataGridView dgv1) {
        int rowIndex = dgv1.SelectedRows[0].Index;  /*得到当前选中行的索引*/

        if (rowIndex == 0)
        {
            MessageBox.Show("已经是第一行了!");
            return;
        }

        List<object> list = new List<object>();
        for (int i = 0; i < dgv1.Columns.Count; i++)
            list.Add(dgv1.SelectedRows[0].Cells[i].Value);   


        for (int i = 0; i < dgv1.Columns.Count; i++)
        {
            dgv1.Rows[rowIndex].Cells[i].Value = dgv1.Rows[rowIndex - 1].Cells[i].Value;/*选中的行*/
            dgv1.Rows[rowIndex - 1].Cells[i].Value = list[i];  /*上一行*/
        }
        dgv1.Rows[rowIndex - 1].Selected = true;
        dgv1.Rows[rowIndex].Selected = false;
    }

    /// <summary>
    /// 选中的项目下移一个
    /// </summary>
    public static void MoveItemDown_DataGridView(DataGridView dgv1) {
        int rowIndex = dgv1.SelectedRows[0].Index;  

        if (rowIndex == dgv1.Rows.Count - 1)
        {
            MessageBox.Show("已经是最后一行了!");
            return;
        }

        List<object> list = new List<object>();/*把当前选中行的数据存入list数组中*/
        for (int i = 0; i < dgv1.Columns.Count; i++)
            list.Add(dgv1.SelectedRows[0].Cells[i].Value);   

        for (int i = 0; i < dgv1.Columns.Count; i++)
        {
            /*选中的行                              下一行  */
            dgv1.Rows[rowIndex].Cells[i].Value = dgv1.Rows[rowIndex + 1].Cells[i].Value;
            dgv1.Rows[rowIndex + 1].Cells[i].Value = list[i];
        }
        dgv1.Rows[rowIndex + 1].Selected = true;
        dgv1.Rows[rowIndex].Selected = false;
    }

    /// <summary>
    /// 移到最顶部
    /// </summary>
    public static void MoveItemTop_DataGridView(DataGridView dgv1) {
        int rowIndex = dgv1.SelectedRows[0].Index;

        if (rowIndex == 0)
        {
            MessageBox.Show("已经是第一行了!");
            return;
        }

        List<object> list = new List<object>();
        for (int i = 0; i < dgv1.Columns.Count; i++)
            list.Add(dgv1.SelectedRows[0].Cells[i].Value); 

        /*选中的行和第一行交换*/
        for (int i = 0; i < dgv1.Columns.Count; i++)
        {
            dgv1.Rows[rowIndex].Cells[i].Value = dgv1.Rows[0].Cells[i].Value; /*选中的行*/
            dgv1.Rows[0].Cells[i].Value = list[i];   /*第一行*/
        }
        dgv1.Rows[0].Selected = true;
        dgv1.Rows[rowIndex].Selected = false;
    }

    /// <summary>
    /// 移动到最底端
    /// </summary>
    public static void MoveItemBottom_DataGridView(DataGridView dgv1) {
        int rowIndex = dgv1.SelectedRows[0].Index;  

        if (rowIndex == dgv1.Rows.Count - 1)
        {
            MessageBox.Show("已经是最后一行了!");
            return;
        }

        List<object> list = new List<object>();
        for (int i = 0; i < dgv1.Columns.Count; i++)
            list.Add(dgv1.SelectedRows[0].Cells[i].Value); 

        
        for (int i = 0; i < dgv1.Columns.Count; i++)
        {
            /*选中的行                              最后一行         交换*/
            dgv1.Rows[rowIndex].Cells[i].Value = dgv1.Rows[dgv1.Rows.Count - 1].Cells[i].Value; 
            dgv1.Rows[dgv1.Rows.Count - 1].Cells[i].Value = list[i];   /*最后一行 =  选中的行*/
        }
        dgv1.Rows[dgv1.Rows.Count - 1].Selected = true;
        dgv1.Rows[rowIndex].Selected = false;
    }



    #endregion