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 there is a SelectionMode property, but changing this merely allows me to select, using Shift+Space, an entire row or column, but this doesn't ha开发者_高级运维ve the same effect as clicking the header with the mouse and causing a sort.
The reason I ask is because I am working on accessibility issues and would like to avoid relying on the mouse.
Thanks for any assistance.
The first thing you will need to do is set the KeyPreview
property to True
in your form properties.
Then in the events you need to add an event handler for the KeyDown()
event
Then add some code something like this:
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);
}
}
May be I'm missing something, but call Sort method at the moment you receive desired Key combination.
i don't know that the column headers ever get focus (ctrl+tab/etc?), but if they could and your keyboard has a context menu button, that could work. but i don't think the headers ever get keyboard focus.
Otherwise, there may be ways to do it using standard accessability features?
精彩评论