Proper way to select previous and next row using a

2019-07-06 17:09发布

Somewhat new to .NET 4.0 C# development and have a quick question. I am working on a basic windows form application where I am using a DataGridView to view data stored in an Access Database.

I would like to use ALT+N to select the next row down and ALT+P to select the next row up (wrapping if possible or maybe just not move at all if already at top of row list or bottom).

I have done a fair amount of looking into this via the internet and I am just not seeing how I can accomplish this, and I am not seeing an option via the properties of the control. :-/

Can anyone provide a solution that will work in a .NET 4.0 C# development environment?

3条回答
放我归山
2楼-- · 2019-07-06 17:32

To Jump To Next Row while Cell are being edited and in between user wants to jump to next rows 1st column i have modified above answer by Chuck Wilbur to suit as per my need and all credit to 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;
            }
        }
        //
查看更多
狗以群分
3楼-- · 2019-07-06 17:33

I ended up using the below event code for a "Next" and "Previous" button making use of the "Alt+N" and "Alt+P" hot keys. Thanks to "Chuck Wilbur" for his contribution as well to this solution.

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

By using the above event code I am able to cycle through the rows in a circular manner in either direction. Works well for me at least.

Thanks again for the help and taking the time to assist "Chuck Wilbur"!

查看更多
Anthone
4楼-- · 2019-07-06 17:41

This may or may not be what you're after, but I think it's heading in the right direction. It at least handles the keys you want and moves the selection with wrapping. It moves the row selection, not the cell selection, so if you want that you'll have to add/change a little code (and take out the this.SelectedRows.Count == 1 check)

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);
    }
}
查看更多
登录 后发表回答