如何在C#Windows Form应用程序的Data gridview中搜索? [英] How to search in Data gridview in C# Windows Form application?

查看:62
本文介绍了如何在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:)

这篇关于如何在C#Windows Form应用程序的Data gridview中搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆