如何移动的GridView选中的行向上/向下的KeyUp或KEYDOWN新闻 [英] How to move gridview selected row up/down on KeyUp or Keydown press

查看:226
本文介绍了如何移动的GridView选中的行向上/向下的KeyUp或KEYDOWN新闻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  1. 用户选择一个行

  2. 会有向上和向下箭头。

  3. 如果用户想向上移动时,用户点击向上箭头按钮

  4. 如果用户要向下移动,然后用户点击向下箭头按钮

  5. 如果该行位于顶部,然后向上箭头按钮将变为禁用

  6. 如果该行是在底部再向下箭头按钮变为禁用



我想这代码,但不是在所有的工作对于上述方案



私人无效KEY_UP(对象发件人,EventArgs五)

  {
如果(dataGridView1.CurrentRow == NULL)回报;
如果(dataGridView1.CurrentRow.Index - 1> = 0)
{

dataGridView1.CurrentCell = dataGridView1.Rows [dataGridView1.CurrentRow.Index - 1] .Cells [ 0];
dataGridView1.Rows .Selected [dataGridView1.CurrentCell.RowIndex] = TRUE;
}
}


解决方案

的方式做的是,
在KEY_UP或扣式卡嗒一声
1分配)获取当前选定行索引
2)将当前选定行至(索引+1)只要索引+ 1小于行计数。



做的KEY_DOWN或按钮点击的否定。

 私人无效dataGridView1_KeyDown(对象发件人,发送KeyEventArgs E)
{
如果(e.KeyCode.Equals(Keys.Up))
{
为moveUp ();
}
如果(e.KeyCode.Equals(Keys.Down))
{
下移();
}
e.Handled = TRUE;
}

私人无效为moveUp()
{
如果(dataGridView1.RowCount大于0)
{
如果(dataGridView1.SelectedRows .Count之间大于0)
{
INT rowCount等= dataGridView1.Rows.Count;
INT指数= dataGridView1.SelectedCells [0] .OwningRow.Index;

如果(指数== 0)
{
的回报;
}
DataGridViewRowCollection行= dataGridView1.Rows;

//删除以前的行并添加所选的行后面。
的DataGridViewRow prevRow =行[指数 - 1];
rows.Remove(prevRow);
prevRow.Frozen = FALSE;
rows.Insert(索引,prevRow);
dataGridView1.ClearSelection();
dataGridView1.Rows [指数 - 1] .Selected =真;
}
}
}

私人无效下移()
{
如果(dataGridView1.RowCount大于0)
{
如果(dataGridView1.SelectedRows.Count大于0)
{
INT rowCount等= dataGridView1.Rows.Count;
INT指数= dataGridView1.SelectedCells [0] .OwningRow.Index;

如果(指数==(rowCount等 - 2))//包括标题行
{
的回报;
}
DataGridViewRowCollection行= dataGridView1.Rows;

//取出下一行并在所选择的行的前面添加。
的DataGridViewRow nextRow =行[索引+ 1];
rows.Remove(nextRow);
nextRow.Frozen = FALSE;
rows.Insert(索引,nextRow);
dataGridView1.ClearSelection();
dataGridView1.Rows [索引+ 1] .Selected =真;
}
}
}

您可以看到我已经分居在上下移动的方法,所以如果你想使用按钮点击事件,而不是主要的向上和向下的关键事件,你可以打电话给他们需要的时候。


  1. The user selects one row
  2. there will be up arrow and down arrow.
  3. If the user wants to move up, the user clicks up arrow button
  4. If the user wants to move down, then the user clicks down arrow button
  5. if the row is at the top then up arrow button becomes disabled
  6. if the row is at the bottom then down arrow button becomes disabled

i tried this code but not at all working for the above scenario

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;
    }
}

解决方案

the way to do is , On key_up or button up clicked 1) Get the current selected row index 2) Set the current selected row to (index + 1) as long as the index +1 is less than the row count.

Do the negation for the key_Down or button down clicked.

    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;
            }
        }
    }

you can see I have separated the move up and down methods, so if you want to use the button clicked events instead of key up and key down event, you can call them when needed.

这篇关于如何移动的GridView选中的行向上/向下的KeyUp或KEYDOWN新闻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆