在数据网格视图中使用键盘输入搜索单词的第一个字母
我的问题是如何搜索以键盘给出的字母开头的单词。我使用下面的代码做到了这一点。
现在我想为下一行执行此操作。找到单词后,我想通过单击向下箭头转到下一行。但是当我单击向下箭头时,所选行将转到上一个所选行。
我该如何解决这个问题?
if (Char.IsLetterOrDigit(e.KeyChar))
{
if (Char.IsLetter(e.KeyChar))
{
for (int i = 0; i < (dgvTestHead.Rows.Count); i++)
{
if (dgvTestHead.Rows[i].Cells["Test_Head"].Value.ToString()
.StartsWith(e.KeyChar.ToString(),
true,
CultureInfo.InvariantCulture))
{
dgvTestHead.ClearSelection();
dgvTestHead.Rows[i].Cells[0].Selected = true;
dgvTestHead.FirstDisplayedScrollingRowIndex = i;
dgvTestHead.Refresh();
return; // stop looping
}
}
}
}
My question is how to search for a word's start with a letter which is given by keyboard. I did this using the code below.
Now I want to do this for the next row. Once I find a word, I want to go to the next row by clicking the down arrow. But when I click the down arrow, the selected row goes to the previous selected row.
How can I solve this?
if (Char.IsLetterOrDigit(e.KeyChar))
{
if (Char.IsLetter(e.KeyChar))
{
for (int i = 0; i < (dgvTestHead.Rows.Count); i++)
{
if (dgvTestHead.Rows[i].Cells["Test_Head"].Value.ToString()
.StartsWith(e.KeyChar.ToString(),
true,
CultureInfo.InvariantCulture))
{
dgvTestHead.ClearSelection();
dgvTestHead.Rows[i].Cells[0].Selected = true;
dgvTestHead.FirstDisplayedScrollingRowIndex = i;
dgvTestHead.Refresh();
return; // stop looping
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为发生这种情况是因为
DataGridView
的CurrentRow
属性没有改变。要将
CurrentRow
设置为所选行(它是只读的),请使用:这对我有用。
I think it happens because the
CurrentRow
property of theDataGridView
doesn't change.To set the
CurrentRow
to the selected row (it is read-only), use:this worked for me.