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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
nikolaoskAuthor Commented:
can i get some code please?
keustermansCommented:
H I am a bit pressed fo time right now.  Give me a day or two and  Iwill send you a code snippet.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

nikolaoskAuthor Commented:
thanks
nikolaoskAuthor Commented:
any news? can anyone help?
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

keustermansCommented:
If you want so select all the rows that start with the letter then you need to remove the firstRowSelected  bit.
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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nikolaoskAuthor Commented:
thanks. it was helpful
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.