在datagridview中搜索数据 [英] Searching data in datagridview
问题描述
我有一个datagridview,它显示数据库表中的数据.有两列ID和NAME. 我有一个文本框,在其中输入名称,这些名称的数据显示在datagridview中.我已经实现了数据搜索,但我想像在comboBox中一样进行搜索.当我键入"a"时,所有以"a"开头的名称都应出现在datagridview中.然后,如果我键入"arn",则所有以"arn"开头的名称都应出现在datagridview中.我需要知道是否有内置的方法或我应该考虑的一些LOGIC. 我正在使用Linq到Sql.
I have a datagridview that displays data from a database table. There are two columns ID and NAME. I have a textbox in which i enter name and data of those names appear in datagridview. I have achieved searching of data but i want to search as done in comboBox. When i type "a" then all names starting with "a" should appear in datagridview. Then if i type "arn" then all names starting with "arn" should appear in datagridview. I need to know if there is a built-in method or some LOGIC that i should consider. I am using Linq to Sql.
EDIT-1
我已经在表单类中创建了一个子类
I have made a subclass in the form class
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事件.
Used BindingList to bind it with dataGridView1. I have applied textChanged event on a textBox1.
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;
这是我正在做的事情,但这将搜索完全相同的名称.我想使它像comboBox下拉搜索.在每个键输入的搜索结果中,将给出包含这些字符的名称/字符串.
This is how i am doing but this searches for exact same name. I want to make it like comboBox drop down search. In which at each key typed the search result gives names/strings containing those characters.
谢谢.
推荐答案
尝试一下
类似于运算符或在LINQ to Entities中使用通配符
http://www.codeproject.com/Articles /634104/Csharp-Wildcard-Search-Using-LINQ
这篇关于在datagridview中搜索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!