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)[
^]
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;
}
}
}
}