How do you jump to a particular row in a DataGridView by typing (a la Windows Explorer details view)
Posted
by russcollier
on Stack Overflow
See other posts from Stack Overflow
or by russcollier
Published on 2010-02-07T17:25:08Z
Indexed on
2010/03/17
20:21 UTC
Read the original article
Hit count: 421
I have a .NET Winforms app in C# with a DataGridView that's read-only and populated with some number of rows. I'd like to have functionality similar to Windows Explorer's (and many other applications) details view for example.
I'd like the DataGridView to behave such that when it has focus if you start typing, the current row selection will jump to the row where the (string) value of cell 0 (i.e. the first column in the row) starts with the characters you typed in.
For example, if I have a DataGridView with 1 column and the following rows:
- Bob
- Jane
- Jason
- John
- Leroy
- Sam
If the DataGridView has focus and I hit the 'b' key on my keyboard, the selected row is now "Bob". If I quickly type in the keys 'ja', the selected row is Jane. If I quickly type in the letters 'jas', the selected row is Jane. If I hit the 'z' key, nothing is selected (since nothing starts with Z).
Likewise if Jane is currently selected and I keep typing the letter 'j', the selection will cycle through to Jason, then John, then back to Jane, each time I hit the 'j' key.
I've been doing some Googling (and "stackoverflowing" :-)) for awhile and cannot find any examples of this type of functionality. I have a rough idea in my head to do this via some sort of short lived timer thread, collecting the keystrokes on KeyPress events for the DataGridView, and selecting rows based on those collected keystrokes matching a Cells[0].Value.StartsWith() type of condition. But it seems like there has to be an easier way that I'm just not seeing.
Any ideas would be much appreciated. Thanks!
© Stack Overflow or respective owner