Albert

Albert

  • NA
  • 42
  • 4.1k

How to focus on cell by typing charachters in datagridview

Jul 7 2017 7:07 AM
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.
 
Code 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
}
}
}

Answers (1)