Is it possible to sort a .NET DataGridView column using only the keyboard?

Is it possible, out of the box, to sort a .NET DataGridView column using only the keyboard?

I understand that there is a SelectionMode property, but changing this just allows me to select, using Shift + Space, an entire row or column, but this does not have the same effect as clicking on the title with the mouse and sorting.

I ask because I am working on accessibility issues and do not want to rely on the mouse.

Thanks for any help.

+5
source share
3 answers

The first thing you need to do is set the property KeyPreviewto Truein your form properties.

KeyDown()

:

public class Form1{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        //sort column 0 descending on a 'D' press
        if (e.KeyCode == Keys.D)
            dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);

        //sort column 0 Ascending on a 'U' press
        if (e.KeyCode == Keys.U)
            dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Ascending);
    }
}
+4

, - , , .

+1

I don’t know that the column headings will ever get focus (ctrl + tab / etc?), But if they could and your keyboard had a context menu button, this could work. but I don’t think the headers will ever get keyboard focus.

Otherwise, there may be ways to do this using standard access features?

0
source