Searching data in datagridview

2019-05-31 08:00发布

问题:

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; }
        }
    }

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;

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.

Thanks.

回答1:

Try this

Entity Framework wildcards & Linq

Like operator or using wildcards in LINQ to Entities

http://www.codeproject.com/Articles/634104/Csharp-Wildcard-Search-Using-LINQ