首页 > 解决方案 > 在不更改 DataGridView 表的情况下搜索 DataGridView

问题描述

我有DataGridView一些数据。我也有一个搜索TextBox,当它的文本发生变化时,我想在DataGridView更改原始数据的情况下进行搜索DataGridView

当客户编写他的搜索行时,DataGridView将执行以下选项之一:

  1. 仅显示与搜索行匹配的行,
  2. 或将客户端跳转到该行并突出显示它(优于 1)。

在 StackOverflow 上搜索时,我可以找到类似的案例,但无法在我的程序中应用他们的任何解决方案。

这是我到目前为止的代码:

private void search_TextChanged(object sender, EventArgs e)
{
    (dataGridView1.DataSource as DataTable).DefaultView.RowFilter =
        string.Format("name = '{0}'", search.Text);
}

但它只显示一个空,DataGridView虽然我应该有一个结果。


我添加到我的 Fund 类函数中搜索列表中的所有名称:

public static List<Fund> findFundbyName(string name)
{
    return funds.Where(c => c.name.Contains(name)).ToList();
}

然后在更新时DataGridView我使用了这个功能:

if (search.Text != "")
{
    table = ConvertListToTable(Fund.funds, search.Text);
}
else
{
    table = ConvertListToTable(Fund.funds, "");
}

标签: c#winforms

解决方案


推荐阅读