How do I use a text box to search for data in a data grid?

Here is 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 empty when I enter something into the textbox. Any idea why? Thank you in advance!

+3


source to share


3 answers


The likely reason you see empty DataGridView

is because of your filter string looking for exact matches with text TextBox

.

"Name='{0}'"

      

Since you are updating this filter in an event TextBox.TextChanged

, no matches were found the first time you enter a character. For example, given the following grid:

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

      

Typing Bar

will give the following results:



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

      

If so, I have provided several options below. If not, then you have a secret.


  • Exact matches: Consider using the following event handler so that the filter is only applied after entering 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);
        }
    }
    
          

  • Starts with matches: for more fluid filtration when text changes:

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

  • Contains matches: again, fluid filtration:

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

+8


source


Just create a new database query where you populated the grid?

Use plain text with LIKE



Edit:

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

+1


source


OhBeWise's 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 you are filtering the rows to be listed but using the column name from the query. the query used to set the datagridview datasource.

For the instance in my example, "LoginID" is in the select statement.

(dataGridViewTheToGrid.DataSource as DataTable).DefaultView.RowFilter = string.Format("LoginID LIKE '{0}%'", textBoxFindUserID.Text);

      

+1


source







All Articles