Shajee Afzal
Shajee Afzal

Reputation: 615

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.

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.

Regards.

Upvotes: 4

Views: 34868

Answers (7)

Łukasz Motyczka
Łukasz Motyczka

Reputation: 1199

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. I did exactly what you are asking for:)

Upvotes: 12

Myo Zaw Oo
Myo Zaw Oo

Reputation: 11

BindingSource bs = new BindingSource();
bs.DataSource = dgrid.DataSource;
bs.Filter = "Full_Name  like '%" + tsptxt_search.Text + "%'";
dgrid.DataSource = bs;

This works for me.

Upvotes: 1

Sabri
Sabri

Reputation: 199

Create a Textbox for search input and use the following code on its TextChanged event

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

Upvotes: 3

Abolfazl Rastgou
Abolfazl Rastgou

Reputation: 832

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

Upvotes: 1

Rae Lee
Rae Lee

Reputation: 1391

 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

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

Upvotes: 0

Fabio
Fabio

Reputation: 32443

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:

Will be better of you show your code of getting data from database,
but this is my shot with manual SQL-query creating:

//...
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

Upvotes: 2

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50752

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

dtData.Select("TicketID = 1")

Also take a look to this post

Upvotes: 0

Related Questions