Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use textbox to search data in data grid view?

Here's my current code:

private void searchTextBox_TextChanged(object sender, EventArgs e)
    {
        (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name='{0}'", searchTextBox.Text);

    }

However my data grid table filters everything and becomes blank whenever I type something into the textbox. Any idea why? Thank you in advance!

like image 974
huehuehue Avatar asked Aug 04 '15 12:08

huehuehue


People also ask

How do I search in DataGrid?

Windows Forms DataGrid (SfDataGrid) provides the support for search the specific string in the SfDataGrid. This can be achieved by using the SearchController. Search method. The founded matches will be highlighted in the SfDataGrid.


4 Answers

The likely reason you are seeing a blank DataGridView is due to your filter string searching for exact matches to the TextBox text.

"Name='{0}'"

Because you are updating this filter in the TextBox.TextChanged event, the first time you enter a character - no matches are found. For example, given the following grid:

╔════╦══════╗                    ╔════════╗
║ ID ║ Name ║      searchTextBox ║        ║
╠════╬══════╣                    ╚════════╝
║ 1  ║ Foo  ║
║ 2  ║ Bar  ║
║ 3  ║ Baz  ║
╚════╩══════╝

Entering Bar will give the following results:

╔════╦══════╗                    ╔════════╗
║ ID ║ Name ║      searchTextBox ║ B      ║
╠════╬══════╣                    ╚════════╝
╚════╩══════╝
╔════╦══════╗                    ╔════════╗
║ ID ║ Name ║      searchTextBox ║ Ba     ║
╠════╬══════╣                    ╚════════╝
╚════╩══════╝
╔════╦══════╗                    ╔════════╗
║ ID ║ Name ║      searchTextBox ║ Bar    ║
╠════╬══════╣                    ╚════════╝
║ 2  ║ Bar  ║
╚════╩══════╝

If this is the case, I've provided a few options below. If this is not the case, then you have a mystery.


  1. Exact matches: consider using the following event handler instead so that the filter is only applied once you've entered the full search text:

    private void searchTextBox_Leave(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(searchTextBox.Text))
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Empty;
        }
        else
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name='{0}'", searchTextBox.Text);
        }
    }
    
  2. StartsWith matches: for more fluid filtering on text changes:

    private void searchTextBox_TextChanged(object sender, EventArgs e)
    {
        (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name LIKE '{0}%'", searchTextBox.Text);
    }
    
  3. Contains matches: again, fluid filtering:

    private void searchTextBox_TextChanged(object sender, EventArgs e)
    {
        (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name LIKE '%{0}%'", searchTextBox.Text);
    }
    
like image 170
OhBeWise Avatar answered Oct 27 '22 08:10

OhBeWise


Simply create a new query against the database in which you populated the grid?

Use the textbox text with LIKE

Edit:

If you want the grid to update with the search, use AJAX.

like image 39
Chris Avatar answered Oct 27 '22 07:10

Chris


OhBeWise answer is the best but until i add something to get points i'm not allowed to like it.

so i'll add this, remember in OhBeWise's answer that your filtering the rows to be listed but using the column name from the query. the query used to set the datasource of the datagridview.

For Instance in my example "LoginID" is in the select statement.

(dataGridViewTheToGrid.DataSource as DataTable).DefaultView.RowFilter = string.Format("LoginID LIKE '{0}%'", textBoxFindUserID.Text);
like image 42
dcarl661 Avatar answered Oct 27 '22 08:10

dcarl661


In addition if you want a multi-column search use this code

(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name LIKE '%{0}%' OR ID LIKE '%{0}%'", searchTextBox.Text);
like image 24
Abdulhakim Zeinu Avatar answered Oct 27 '22 07:10

Abdulhakim Zeinu