按键时如何逐个搜索网格中的单元格c#
?
我尝试了key press event
几次,但没有奏效。
有一个例外。
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))
{
list.Add(dataGridView1.Rows[i].Cells[0].Value.ToString());
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == list[i].ToString())
{
dataGridView1.Rows[i].Cells[0].Selected = true;
}
//dataGridView1.Rows[i].Cells[0].Selected = true;
/*if (dataGridView1.Rows[i].Cells[0].Selected == true)
{
dataGridView1.Rows[i].Cells[0].Selected = false;
} */
// stop looping
}
}
}
}