在用户按键上搜索datagridview [英] Search datagridview on user keypress
问题描述
我正在尝试使用用户按下的相同键盘选择单元格值开始的第一行。这是给我麻烦的部分。
I'm trying to select the first row where the cell value starts with the same keychar the user pressed. That's the part that is giving me trouble.
以下是我正在处理事件(使用工作解决方案更新):
Here's how I'm handling the event (updated with working solution):
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
}
}
}
}
I'm sure it's something simple that I'm overlooking, but for the life of me can't figure out what it is.
使用解决方案更新代码
推荐答案
可能是一个案例问题,是单元格中的值[名称]从大写字母开始?尝试在两者上使用ToUpper或ToLower;或者你可以尝试StartsWith(e.KeyChar,true)来ignoreCase。如果您尝试选择该行,则需要执行dataGridView1.Rows [i] .Selected = true
Might be a case issue, is the Value in Cells["Name"] start with a capital letter? Try using ToUpper or ToLower on both; or you could try StartsWith(e.KeyChar, true) to ignoreCase. If you are trying to select the row, you'll want to do dataGridView1.Rows[i].Selected = true
这篇关于在用户按键上搜索datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!