Click here to Skip to main content
15,891,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to search a cell in a grid one by one when key press in c#?

I was try the key press event several times but didn't work.
Posted
Comments
Maciej Los 2-Dec-13 2:19am    
WebControls? WinForms? WPF?
What have you tried till now?
"I was try the key press event several times but didn't work" - is not informative at all ;(
Javad Emsakpour 2-Dec-13 4:26am    
for winforms
like this...
private int lastIndex;
private char lastKey;

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[0].Value.ToString().StartsWith(e.KeyChar.ToString(), true, CultureInfo.InvariantCulture))
{
if (lastKey == e.KeyChar && lastIndex < i)
{
continue;
}

lastKey = e.KeyChar;
lastIndex = i;
dataGridView1.Rows[i].Cells[0].Selected = true;
return;
}
}
}
}

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