C# 在不更改DataGridView表的情况下搜索DataGridView
我有一个带有一些数据的C# 在不更改DataGridView表的情况下搜索DataGridView,c#,winforms,C#,Winforms,我有一个带有一些数据的DataGridView。我还有一个搜索TextBox,当它的文本发生变化时,我想在DataGridView中搜索,而不改变原始DataGridView中的数据 当客户端写入其搜索行时,DataGridView将执行以下选项之一: 仅显示与搜索行匹配的行 或者将客户端跳转到行并高亮显示它(优于1) 在搜索StackOverflow时,我可以找到类似的情况,但无法在我的程序中应用它们的任何解决方案 这是我目前掌握的代码: private void search_TextCha
DataGridView
。我还有一个搜索TextBox
,当它的文本发生变化时,我想在DataGridView
中搜索,而不改变原始DataGridView
中的数据
当客户端写入其搜索行时,DataGridView
将执行以下选项之一:
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,虽然我应该有一个结果。你是如何初始化
Datasource
属性的?你是什么意思?这是:string.Format(“name=”{0}',search.Text)代码>尝试使用精确匹配进行筛选。如果未找到匹配项,则结果为空DataView。可能您需要类似于string.Format(“名称类似“{0}%””,search.Text)的内容代码>(部分匹配)。过滤器是在TextChanged事件中生成的,因此。。。
if (search.Text != "")
{
table = ConvertListToTable(Fund.funds, search.Text);
}
else
{
table = ConvertListToTable(Fund.funds, "");
}