4

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.

Shajee Afzal
  • 595
  • 4
  • 7
  • 14

7 Answers7

12

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:)

Artur Carvalho
  • 6,901
  • 10
  • 76
  • 105
Łukasz Motyczka
  • 1,169
  • 2
  • 13
  • 35
  • Thank you for your help but when I try to implement it, it shows this error : "Cannot interpret token '{' at position 27." – Shajee Afzal Feb 18 '14 at 04:34
  • Also I found a comment that may be useful: _It looks like I am getting error: "Cannot interpret token '{' at position 7" because the SQLDataSource I am binding the WebDataGrid to is being filtered by a control (dropdownbox). The FilterExpresion of the SQLDataSource is set to: "MyFieldName = {0}". Once I delete the filter expression the error no longer somes up. The problem is I need to filter the SQLDataSource whenever this dropdownbox changes its Selected Value._ – Łukasz Motyczka Feb 18 '14 at 06:49
  • If there any option to search entire GridView I don't want to specify columnName – Sunil Acharya Mar 22 '17 at 06:04
3

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);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Sabri
  • 189
  • 1
  • 1
  • 13
2

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
Fabio
  • 31,528
  • 4
  • 33
  • 72
1
BindingSource bs = new BindingSource();
bs.DataSource = dgrid.DataSource;
bs.Filter = "Full_Name  like '%" + tsptxt_search.Text + "%'";
dgrid.DataSource = bs;

This works for me.

Pang
  • 9,564
  • 146
  • 81
  • 122
Myo Zaw Oo
  • 11
  • 1
1
BindingSource bs = new BindingSource(); 
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "[database column Name To Search] like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;
Abolfazl Rastgou
  • 655
  • 10
  • 13
  • Giving some explanatory text and not only sourcecode would make your answer even better. This is what we expect from guys that get +50 rep on a single day;-) – Marged Mar 24 '18 at 19:45
0

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

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
0
 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;
Rae Lee
  • 1,321
  • 10
  • 11