如何在C#Windows Form应用程序的Data gridview中搜索?

问题描述:

我有一个表单,其中简单的gridview由数据库中的表填充,该表具有如TicketID,Name,Company,Product等列.现在,我想添加搜索功能,以便用户可以按客户名称或公司进行搜索,或者票证ID.

I have a form in which a simple gridview is populated by a table in database having columns like TicketID, Name, Company, Product etc. Now I want to add a search feature so that user could search by customer name or company or TicketID.

我该怎么做?我想在数据网格上方放置一个combox框,一个文本框和一个简单的搜索"按钮.例如,当用户选择TicketID时,在文本框中输入"1"并按搜索",则应该使用TicketID = 1的条目刷新数据网格.

How can I do that ? I want to place a combox box, textbox and a simple "search" button above datagrid. When the user selects TicketID for example, enters "1" in textbox and presses "Search", it should refresh datagrid with entry where TicketID = 1.

现在我对如何实现它一无所知.用谷歌搜索,但没有发现有用的东西.因此,在这方面的任何帮助将不胜感激.

Now I don't have any idea on how to implement it. Googled for it but found nothing useful. So any help in this regard will be appreciated.

致谢.

您可以查看:

BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = columnNameToSearch + " like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;

这将在您选择的列中显示包含textbox1中文本的记录.我完全按照您的要求去做:)

This will show you records containing text from textbox1 in column of your choice. I did exactly what you are asking for:)