• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1116
  • Last Modified:

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
0
nikolaosk
Asked:
nikolaosk
  • 6
  • 4
1 Solution
 
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
 
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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
 
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
 
nikolaoskAuthor Commented:
thanks. it was helpful
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now