【发布时间】:2014-11-11 14:34:19
【问题描述】:
我有一个显示数据库表中数据的数据网格视图。有两列 ID 和 NAME。 我有一个文本框,我在其中输入名称,这些名称的数据出现在 datagridview 中。我已经实现了数据的搜索,但我想像在组合框中所做的那样进行搜索。当我键入“a”时,所有以“a”开头的名称都应该出现在 datagridview 中。然后,如果我输入“arn”,那么所有以“arn”开头的名称都应该出现在 datagridview 中。我需要知道是否有我应该考虑的内置方法或一些逻辑。 我正在使用 Linq to Sql。
EDIT-1
我在表单类中做了一个子类
public class Table1
{
public int ID;
public string Name;
public MyList(string _newID, string _newName)
{
_id = _newID;
_name = _newName;
}
public int _id
{
get { return ID; }
set { ID = value; }
}
public string _name
{
get { return Name; }
set { Name = value; }
}
}
使用 BindingList 将其与 dataGridView1 绑定。我在 textBox1 上应用了 textChanged 事件。
BindingList<Table1> tempData = new BindingList<Table1>();
string name = textBox1.Text;
var result = from row in context.Tables
where row.Name == name
select row;
foreach (Table std in result)
{
tempData.Add(new MyList(row.ID, row.Name);
}
dataGridView1.DataSource = tempData;
这就是我正在做的,但这会搜索完全相同的名称。我想让它像组合框下拉搜索。在每个键入的键处,搜索结果都会给出包含这些字符的名称/字符串。
谢谢。
【问题讨论】:
-
向我们展示您到目前为止所做的事情。向我们提供一些示例代码,然后从您的代码中可以启发您的方法。
-
如果你有datagridview绑定到的bindingsource,你可以使用bindingsource的'filter'属性。并在每次更改文本框时进行过滤。
-
@WillMarcouiller!我已经更新了我的帖子。等待帮助。
-
你想尝试使用 linq 进行通配符搜索
标签: c# search linq-to-sql datagridview