How to use the keyboard to highlight rows in a DataGridView

hello experts,

i have developed an application in .net,WinForm  with C# .•¤ 2.0. i am returning through a DataGridView control records from a database in my form.

I need when ,for example, the user hits the "T" letter in the keyboard the first row in the DataGridView that starts with "T" to be selected.

i want the same behaviour that exists when we hit a letter in a windows explorer window and the first file-folder that starts with that letter is selected.

thanks a lot
nikolaoskAsked:
Who is Participating?
 
keustermansCommented:
Forgive me I had to do a rush job with this.

But the while loop is not the correct way of doing this you need to do it as follws as the while loop will not accomplish selecting a singele row
private void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            string c = ((char)e.KeyValue).ToString();
            bool firstRowSelected = false;
 
            
                if (!dataGridView1.CurrentCell.IsInEditMode)
                {
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        row.Selected = false;
                        if (row.Cells[0].Value != null)
                        {
                            if (row.Cells[0].Value.ToString().ToLower().StartsWith(c.ToLower())&& !firstRowSelected)
                            {
                                firstRowSelected=true;
                                row.Selected = true;
 
                            }
                        }
 
                    }
                }
            
        }

Open in new window

0
 
keustermansCommented:
What you need to do here is to intercept the key down event  when the datagrid has focus and search for the respective row in the datagrid and select it.

You must however remember to manage this carefully as you do not want this to happen while editing a row. You can check this maybe by using IsCurrentCellInEditMode property and if it si you hande the key down event as such.
0
 
nikolaoskAuthor Commented:
can i get some code please?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
keustermansCommented:
H I am a bit pressed fo time right now.  Give me a day or two and  Iwill send you a code snippet.
0
 
nikolaoskAuthor Commented:
thanks
0
 
nikolaoskAuthor Commented:
any news? can anyone help?
0
 
keustermansCommented:
Sory I forgot about the question.

Add the following event your datagrid.



 private void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            string c = ((char)e.KeyValue).ToString();
            bool firstRowSelected = false;
 
            while (!firstRowSelected)
            {
                if (!dataGridView1.CurrentCell.IsInEditMode)
                {
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        row.Selected = false;
                        if (row.Cells[0].Value != null)
                        {
                            if (row.Cells[0].Value.ToString().ToLower().StartsWith(c.ToLower()))
                            {
                                firstRowSelected = true;
                                row.Selected = true;
 
                            }
                        }
 
                    }
                }
            }
        }

Open in new window

0
 
keustermansCommented:
If you want so select all the rows that start with the letter then you need to remove the firstRowSelected  bit.
0
 
keustermansCommented:
Oops, forgot to mention that the grid needs to be readonly or editmode needs to be EditOnF2 and obviously the grid has to have focus.
0
 
nikolaoskAuthor Commented:
thanks. it was helpful
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.