Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to focus on cell which contains pressed characters.

Assume datagridview which contains two column Name and Address.
Now in Name column there is lot of records Like Nims, john, kan, rocks, rita, etc...

Now if I am entering character 'K','A','N' then cell will be focus on kan.

I have google for this but I get solution as below code which didn't satisfied my question.

Because it is focus cell which contains pressed character as starting character of cell value.

Thanks in advance.

What I have tried:

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Char.IsLetter(e.KeyChar))
    {
        for (int i = 0; i < (dataGridView1.Rows.Count); i++)
        {
            if (dataGridView1.Rows[i].Cells["Name"].Value.ToString().StartsWith(e.KeyChar.ToString(), true, CultureInfo.InvariantCulture))
            {
                dataGridView1.Rows[i].Cells[0].Selected = true;
                return; // stop looping
            }
        }
    }
}
Posted
Updated 7-Jul-17 3:48am

You can define a public variable to hold the typed characters and use that in your dataGridView1_KeyPress(). For KeyCodes see: Keys Enumeration (System.Windows.Forms)[^]
C#
public string typedChars = string.Empty;

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
    {
        typedChars = string.Empty;
        return;
    }

    if (Char.IsLetter(e.KeyChar))
    {
        typedChars += e.KeyChar.ToString();

        for (int i = 0; i < (dataGridView1.Rows.Count); i++)
        {
            if (dataGridView1.Rows[i].Cells["Name"].Value.ToString().StartsWith(typedChars, true, CultureInfo.InvariantCulture))
            {
                dataGridView1.Rows[i].Cells[0].Selected = true;
                return; // stop looping
            }
        }
    }
}
 
Share this answer
 
v3
Comments
Member Albert 7-Jul-17 12:04pm    
Thanks RickZeeland,
I want to reset it on when user press 'UP', 'DOWN', 'LEFT', 'RIGHT' arrow key.
So what should to do?
RickZeeland 7-Jul-17 13:52pm    
I updated the solution to reset the variable. Another crazy idea would be to reset the variable after a period of x seconds of no keyboard activity.
Member Albert 7-Jul-17 23:13pm    
Thanks Bro,
Now its all clear.
I have done this before you comment, but I have done with by creating individual dataGridView1_KeyDown method.
By the way it has solved now.
Thanks again.
vaishali jadhav 2022 1-Sep-22 3:42am    
I m Trying your this code public string typedChars = string.Empty; private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) { typedChars = string.Empty; return; } if (Char.IsLetter(e.KeyChar)) { typedChars += e.KeyChar.ToString(); for (int i = 0; i < (dataGridView1.Rows.Count); i++) { if (dataGridView1.Rows[i].Cells["Name"].Value.ToString().StartsWith(typedChars, true, CultureInfo.InvariantCulture)) { dataGridView1.Rows[i].Cells[0].Selected = true; return; // stop looping } } } }

but when i excute else part get error or i comment else part so if i type another value which is not exist ge null exception error
Another option would be to use LINQ wildcard search, here is an example:
C# - Wildcard Search Using LINQ[^]
 
Share this answer
 
Comments
Member Albert 7-Jul-17 12:06pm    
No.! I dont want textbox for search data in datagridview.
I just want to focus Cell which contains value as typed in datagridview only.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900