如何在KeyUp或Keydown按下上移/下移gridview选择的行

san*_*eev 3 c#

在此输入图像描述

  1. 用户选择一行
  2. 会有向上箭头和向下箭头.
  3. 如果用户想要向上移动,则用户单击向上箭头按钮
  4. 如果用户想要向下移动,则用户单击向下箭头按钮
  5. 如果行位于顶部,则向上箭头按钮将被禁用
  6. 如果行位于底部,则向下箭头按钮将被禁用

我尝试了这段代码,但根本没有为上述场景工作

private void key_up(object sender,EventArgs e)

{
    if (dataGridView1.CurrentRow == null) return;
    if (dataGridView1.CurrentRow.Index - 1 >= 0)
    {

        dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index - 1].Cells[0];
        dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

Jeg*_*gan 16

要做的方法是,单击on key_up或按钮向上1)获取当前选定的行索引2)只要索引+1小于行计数,就将当前所选行设置为(index + 1).

单击key_Down或按钮的否定.

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode.Equals(Keys.Up))
        {
            moveUp();
        }
        if (e.KeyCode.Equals(Keys.Down))
        {
            moveDown();
        }
        e.Handled = true;
    }

    private void moveUp()
    {
        if (dataGridView1.RowCount > 0)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int rowCount = dataGridView1.Rows.Count;
                int index = dataGridView1.SelectedCells[0].OwningRow.Index;

                if (index == 0)
                {
                    return;
                }
                DataGridViewRowCollection rows = dataGridView1.Rows;

                // remove the previous row and add it behind the selected row.
                DataGridViewRow prevRow = rows[index - 1];
                rows.Remove(prevRow);
                prevRow.Frozen = false;
                rows.Insert(index, prevRow);
                dataGridView1.ClearSelection();
                dataGridView1.Rows[index - 1].Selected = true;
            }
        }
    }

    private void moveDown()
    {
        if (dataGridView1.RowCount > 0)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int rowCount = dataGridView1.Rows.Count;
                int index = dataGridView1.SelectedCells[0].OwningRow.Index;

                if (index == (rowCount - 2)) // include the header row
                {
                    return;
                }
                DataGridViewRowCollection rows = dataGridView1.Rows;

                // remove the next row and add it in front of the selected row.
                DataGridViewRow nextRow = rows[index + 1];
                rows.Remove(nextRow);
                nextRow.Frozen = false;
                rows.Insert(index, nextRow);
                dataGridView1.ClearSelection();
                dataGridView1.Rows[index + 1].Selected = true;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

你可以看到我已经分离了上下移动方法,因此如果你想使用按钮点击事件而不是按键和按键事件,你可以在需要时调用它们.


Mar*_*rio 5

清理了Jegan的代码并使其适用于多个datagridviews.

    private static void MoveUp(DataGridView dgv)
    {
        if (dgv.RowCount <= 0) 
            return;

        if (dgv.SelectedRows.Count <= 0) 
            return;

        var index = dgv.SelectedCells[0].OwningRow.Index;

        if (index == 0) 
            return; 

        var rows = dgv.Rows;
        var prevRow = rows[index - 1];
        rows.Remove(prevRow);
        prevRow.Frozen = false;
        rows.Insert(index, prevRow);
        dgv.ClearSelection();
        dgv.Rows[index - 1].Selected = true;
    }

    private static void MoveDown(DataGridView dgv)
    {
        if (dgv.RowCount <= 0) 
            return;

        if (dgv.SelectedRows.Count <= 0) 
            return;

        var rowCount = dgv.Rows.Count;
        var index = dgv.SelectedCells[0].OwningRow.Index;

        if (index == (rowCount - 2)) // include the header row
            return;

        var rows = dgv.Rows;
        var nextRow = rows[index + 1];
        rows.Remove(nextRow);
        nextRow.Frozen = false;
        rows.Insert(index, nextRow);
        dgv.ClearSelection();
        dgv.Rows[index + 1].Selected = true;
    }
Run Code Online (Sandbox Code Playgroud)