如何在DataGridView的不同行中搜索相同的文本? [英] How to search the same text in different Rows in a DataGridView?

查看:65
本文介绍了如何在DataGridView的不同行中搜索相同的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索按钮,当我在文本框中键入内容并单击按钮时,它将在DataGridView的品牌列中进行搜索。

如果键入 BMW ,则选择该行,但是如果我有多个 BMW ,并再次单击该按钮,则不会选择其他行:

I have a search button, when I type in my TextBox and click the Button, it search in my "Brand" column in a DataGridView.
If I type BMW, the row gets selected, however if I have multiple BMWs, and click the button again, the other rows won't be selected:

string searchValue = textBox1.Text;

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells[1].Value.ToString().Equals(searchValue))
        {
            row.Selected = true;
            break;
        }
        dataGridView1.ClearSelection();
    }
}
catch (Exception exc)
{
    MessageBox.Show("Not found");
}


推荐答案

设置<$ c $之后c> MultiSelect 属性设置为 True ,将 SelectionMode 设置为 FullRowSelect ,就可以实现。

After setting MultiSelect property to True and SelectionMode to FullRowSelect, you can achieve it.

dataGridView1.MultiSelect = true;
dataGridView1.SelectionMode = FullRowSelect:

还有另一件事,删除 break 语句中的语句,因为它在找到第一个匹配项时中断了循环。
请尝试一下,让我知道是否需要任何其他更改。

And one more thing, remove the break statement from your code as it breaks your loop when the first match is found. Please try this and let me know if any additional change is required in it.

if(row.Cells[1].Value.ToString().Equals(searchValue))
{
    row.Selected = true;
    //break; -- remove this one and try
}

这篇关于如何在DataGridView的不同行中搜索相同的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆