通过DataGridView使用热键选择上一行和下一行的正确方法

时间:2012-02-22 21:38:04

标签: c# select datagridview row hotkeys

.NET 4.0 C#开发有点新鲜,并且有一个快速的问题。我正在使用基本的Windows窗体应用程序,我使用DataGridView来查看存储在Access数据库中的数据。

我想使用ALT + N来选择下一行和ALT + P来选择下一行(如果可能,可以包装,如果已经在行列表的顶部或底部,则可能根本不移动)。 / p>

我已经通过互联网做了相当多的研究,我只是没有看到我如何能够实现这一点,而且我没有通过控件的属性看到一个选项。 : - /

任何人都可以提供适用于.NET 4.0 C#开发环境的解决方案吗?

3 个答案:

答案 0 :(得分:2)

我最终使用下面的事件代码,使用“下一步”和“上一页”按钮,使用“Alt + N”和“Alt + P”热键。感谢“Chuck Wilbur”对这个解决方案的贡献。

private void btnNext_Click(object sender, EventArgs e)
        {
            //Get number of records displayed in the data grid view and subtract one to keep in line with index that starts with 0
            int numOfRows = dataGrdViewCases.Rows.Count - 1;

            //Get current row selected
            int index = dataGrdViewCases.SelectedRows[0].Index;

            // Determine if the next record exists or cycle back to the first record in the set
            if (index < numOfRows)
            {
                //Change the selected row to next row down in the data set
                dataGrdViewCases.CurrentCell = dataGrdViewCases[0, index + 1];
            }
            else
            {
                // Select the first record of the data set
                dataGrdViewCases.CurrentCell = dataGrdViewCases[0, 0];
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            //Get number of records displayed in the data grid view and subtract one to keep in line with index that starts with 0
            int numOfRows = dataGrdViewCases.Rows.Count - 1;

            //Get current row selected
            int index = dataGrdViewCases.SelectedRows[0].Index;

            // Determine if the previous record exists or cycle back to the last record in the set
            if (index != 0)
            {
                //Change the selected row to next row down in the data set
                dataGrdViewCases.CurrentCell = dataGrdViewCases[0, index - 1];
            }
            else
            {
                // Select the first record of the data set
                dataGrdViewCases.CurrentCell = dataGrdViewCases[0, numOfRows];
            }
        }

通过使用上述事件代码,我可以在任一方向上以循环方式循环遍历行。至少对我有用。

再次感谢您的帮助并花时间协助“Chuck Wilbur”!

答案 1 :(得分:0)

这可能是也可能不是你所追求的,但我认为它正朝着正确的方向前进。它至少处理你想要的键并用包装移动选择。它移动行选择,而不是单元格选择,所以如果你想要,你必须添加/更改一些代码(并取出this.SelectedRows.Count == 1检查)

class RowSelectDataGridView : DataGridView
{
    protected override bool ProcessDialogKey(Keys keyData)
    {
        if ((keyData & Keys.Alt) == Keys.Alt && this.SelectedRows.Count == 1)
        {
            int selIndex = this.SelectedRows[0].Index;
            int newSelIndex = selIndex + 1;
            if ((keyData & Keys.N) == Keys.N)
            {
                if (newSelIndex >= Rows.Count) newSelIndex = 0;
            }
            else if ((keyData & Keys.P) == Keys.P)
            {
                newSelIndex = selIndex - 1;
                if (newSelIndex < 0) newSelIndex = Rows.Count - 1;
            }
            else return base.ProcessDialogKey(keyData);

            this.SetSelectedRowCore(selIndex, false);
            this.SetSelectedRowCore(newSelIndex, true);
            return true;
        }
        return base.ProcessDialogKey(keyData);
    }
}

答案 2 :(得分:0)

在正在编辑单元格的同时跳转到下一行,并且在用户想要跳转到下一行之间的第一列我已经修改了Chuck Wilbur的上述答案以符合我的需要并且归功于Chuck Wilbur

//Jump To Next & Prior Row
        if (this.CurrentRow != null)
        {
            if ((keyData & Keys.Alt) == Keys.Alt)
            {
                int selIndex = this.CurrentRow.Index;
                int newSelIndex = selIndex + 1;
                if ((keyData & Keys.N) == Keys.N)
                {
                    if (newSelIndex >= Rows.Count) newSelIndex = 0;
                }
                else if ((keyData & Keys.P) == Keys.P)
                {
                    newSelIndex = selIndex - 1;
                    if (newSelIndex < 0) newSelIndex = Rows.Count - 1;
                }
                else return base.ProcessDialogKey(keyData);

                this.SetSelectedRowCore(selIndex, false);
                this.SetSelectedRowCore(newSelIndex, true);
                this.CurrentCell = this[0, newSelIndex];
                this.SetSelectedRowCore(newSelIndex, false);
                return true;
            }
        }
        //