Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过DataGridView使用热键选择上一行和下一行的正确方法_C#_Select_Datagridview_Row_Hotkeys - Fatal编程技术网

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

C# 通过DataGridView使用热键选择上一行和下一行的正确方法,c#,select,datagridview,row,hotkeys,C#,Select,Datagridview,Row,Hotkeys,对.NET4.0c#开发有些陌生,我有一个快速的问题。我正在开发一个基本的windows窗体应用程序,使用DataGridView查看存储在Access数据库中的数据 我希望使用ALT+N向下选择下一行,使用ALT+p向上选择下一行(如果可能,请换行,如果已经在行列表顶部或底部,则可能根本不移动) 我已经通过互联网对此进行了大量的研究,我只是不知道如何才能做到这一点,我也没有看到通过控件属性的选项:-/ 有谁能提供一个能在.NET 4.0 C#开发环境下工作的解决方案吗?这可能是您想要的,也可能

对.NET4.0c#开发有些陌生,我有一个快速的问题。我正在开发一个基本的windows窗体应用程序,使用DataGridView查看存储在Access数据库中的数据

我希望使用ALT+N向下选择下一行,使用ALT+p向上选择下一行(如果可能,请换行,如果已经在行列表顶部或底部,则可能根本不移动)

我已经通过互联网对此进行了大量的研究,我只是不知道如何才能做到这一点,我也没有看到通过控件属性的选项:-/


有谁能提供一个能在.NET 4.0 C#开发环境下工作的解决方案吗?

这可能是您想要的,也可能不是,但我认为它正朝着正确的方向发展。它至少可以处理所需的关键点,并通过换行移动选择。它会移动行选择,而不是单元格选择,因此如果需要,您必须添加/更改一点代码(并取出
this.SelectedRows.Count==1
check)

class RowSelectDataGridView:DataGridView
{
受保护的覆盖布尔ProcessDialogKey(Keys keyData)
{
if((keyData&Keys.Alt)=Keys.Alt&&this.SelectedRows.Count==1)
{
int selIndex=this.SelectedRows[0]。索引;
int newSelIndex=selIndex+1;
if((keyData和Keys.N)=Keys.N)
{
如果(newselinex>=Rows.Count)newselinex=0;
}
else if((keyData&Keys.P)=Keys.P)
{
newSelIndex=selIndex-1;
如果(newselinex<0)newselinex=Rows.Count-1;
}
else返回base.ProcessDialogKey(keyData);
此.SetSelectedRowCore(selIndex,false);
此.SetSelectedRowCore(newseIndex,true);
返回true;
}
返回base.ProcessDialogKey(keyData);
}
}

最后,我利用“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];
            }
        }
private void btnNext\u单击(对象发送方,事件参数e)
{
//获取数据网格视图中显示的记录数,并减去一以与以0开头的索引保持一致
int numorrows=dataGrdViewCases.Rows.Count-1;
//选择当前行
int index=dataGrdViewCases.SelectedRows[0]。index;
//确定下一条记录是否存在或循环回集合中的第一条记录
如果(索引
通过使用上面的事件代码,我能够以循环方式在任意方向上遍历行。至少对我来说效果不错


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

在编辑单元格时跳到下一行,中间用户想跳到下一行第1列我已修改了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;
            }
        }
        //
//跳到下一行和上一行
如果(this.CurrentRow!=null)
{
if((keyData&Keys.Alt)=Keys.Alt)
{
int selIndex=this.CurrentRow.Index;
int newSelIndex=selIndex+1;
if((keyData和Keys.N)=Keys.N)
{
如果(newselinex>=Rows.Count)newselinex=0;
}
else if((keyData&Keys.P)=Keys.P)
{
newSelIndex=selIndex-1;
如果(newselinex<0)newselinex=Rows.Count-1;
}
else返回base.ProcessDialogKey(keyData);
此.SetSelectedRowCore(selIndex,false);
此.SetSelectedRowCore(newseIndex,true);
this.CurrentCell=this[0,newselinex];
此.SetSelectedRowCore(newseIndex,false);
返回true;
}
}
//

谢谢!对于C#Windows窗体编程来说,我是个新手,看到你的代码我有点不知所措了!:)不过我还是坚持了下来,我使用的解决方案是作为我自己帖子的答案发布的。请复习并让我知道你的想法。我在表单中添加了“下一步”和“上一步”按钮,并在按下或“Alt+N”或“Alt+P”时添加了事件代码。干杯@Tommy2Fast我想如果它能工作,那就太好了,不过如果你不得不为一个界面这样做,而一些固执己见的设计师不希望你添加额外的按钮或其他东西,那么你所拥有的将无法工作。我想我只是对你保留了我解决方案中微不足道的部分感到失望
//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;
            }
        }
        //