简体   繁体   English

如何在C#Windows窗体应用程序中搜索数据gridview?

[英]How to search in Data gridview in C# Windows Form application?

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. 我有一个表单,其中一个简单的gridview由数据库中的表填充,其中包含TicketID,Name,Company,Product等列。现在我想添加一个搜索功能,以便用户可以按客户名称或公司或TicketID进行搜索。

How can I do that ? 我怎样才能做到这一点 ? I want to place a combox box, textbox and a simple "search" button above datagrid. 我想在数据网格上方放置一个combox框,文本框和一个简单的“搜索”按钮。 When the user selects TicketID for example, enters "1" in textbox and presses "Search", it should refresh datagrid with entry where TicketID = 1. 例如,当用户选择TicketID时,在文本框中输入“1”并按“搜索”,它应刷新数据网格,其中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. 所以在这方面的任何帮助将不胜感激。

Regards. 问候。

You may look into: 你可以看看:

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

This will show you records containing text from textbox1 in column of your choice. 这将显示包含您选择的列中textbox1的文本的记录。 I did exactly what you are asking for:) 我完全按照你的要求做了:)

If you want refresh your DataSource depended on the search parameters, then you need to build a new SQL query depended on then "search" controls: 如果你想根据搜索参数刷新你的DataSource ,那么你需要根据当时的“搜索”控件构建一个新的SQL查询:

Will be better of you show your code of getting data from database, 你会更好地展示从数据库获取数据的代码,
but this is my shot with manual SQL-query creating: 但这是我用手动SQL查询创建的镜头:

//...
StringBuilder query = new StringBuilder();
query.AppendLine("SELECT TicketID, Name, Company, Product");
query.AppendLine("FROM YourTable WHERE 1=1");
if (txtSearch.TextLength > 0)
{
    query.AppendLine("AND TicketID = @TicketID");
    //Here add sqlparameter with textbox value
}
//... and so on
BindingSource bs = new BindingSource();
bs.DataSource = dgrid.DataSource;
bs.Filter = "Full_Name  like '%" + tsptxt_search.Text + "%'";
dgrid.DataSource = bs;

This works for me. 这适合我。

BindingSource bs = new BindingSource(); 
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "[database column Name To Search] like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;

What you need, filtering, not searching... Searching is highlighting the correct row from the set 你需要什么,过滤,而不是搜索......搜索突出显示集合中的正确行

Set grid DataSource to the result of DataTable.Select method 将grid DataSource设置为DataTable.Select方法的结果

dtData.Select("TicketID = 1")

Also take a look to this post 另外看看这篇文章

 DataSet ds;
 DataView dv;

 public Form1()
    {
        InitializeComponent();
        ds = new DataSet();
       dv = new DataView();
    } 

private void Form1_Load(object sender, EventArgs e)
{
    ds=SelectStudents();            
    dv.Table = ds.Tables[0];
    dataGridView1.DataSource = dv; 
}

Now in the text_changed event of the textbox, write below code 现在在文本框的text_changed事件中,写下面的代码

dv.RowFilter = "StudentName like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = dv;

Create a Textbox for search input and use the following code on its TextChanged event 为搜索输入创建文本框,并在其TextChanged事件上使用以下代码

private void txtSearch_TextChanged(object sender, EventArgs e)
{
    (dataGridView.DataSource as DataTable).DefaultView.RowFilter = string.Format("TicketID like '{0}%' OR Product like '{0}%' OR Name like '{0}%' OR Product like '{0}%'", txtSearch.Text);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ADO.Net数据绑定到Windows窗体应用程序C#中的gridview - ADO.Net data binding to gridview in Windows form application C# 从数据表中选择特定的列并与Windows窗体应用程序的数据GridView C#绑定 - Select particular column from Datatable and bind with Data GridView C#, Windows form Application 如何将剪贴板数据粘贴到c#windows窗体中的另一个应用程序 - How to paste clipboard data to another application in c# windows form 如何从数据库C#Windows Form应用程序中检索数据? - How to Retrieve data from a database c# windows form application? 在C#Windows窗体中以PDF格式获取Gridview数据? - Get Gridview Data in PDF Format in C# Windows Form? 如何在 C# windows 应用程序中从数据 gridview 条目插入记录到 SQL 数据库? - How to insert records from data gridview entry to SQL database in C# windows application? 如何在C#Windows应用程序中通过计时器控件刷新Gridview? - How to Refresh a Gridview by a Timer Control in C# Windows Application? 在c#中创建搜索功能,windows窗体应用程序 - Creating a search function in c#, windows form application 如何在C#Windows窗体应用程序中使用VideoBrush? - How to VideoBrush in c# windows form application? 如何在C#应用程序中运行Windows窗体 - How to run a Windows Form in a C# application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM