Click here to Skip to main content
15,169,285 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hey guys

I have a dataGridView with many entries. I would like to filter all entries with a text from a textbox, example:

TextBox text = "Mr.", when I click the button "Search", the whole list in dataGridView shoult only show entries with the title like 'Mr.'.


Can anyone help me, please?


Thanks for every help.
valentigra
Posted
Updated 23-Feb-21 21:31pm
Comments
Naz_Firdouse 10-Jan-14 2:45am
   
what have you tried so far???
v_tigra 10-Jan-14 2:56am
   
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = dataGridView1.Columns[1].HeaderText.ToString() + " LIKE '%" + searchTextBox.Text + "%'";
dataGridView1.DataSource = bs;

but it doesn't help..

In the click event of the button check the content of the textbox and use the content to filter the underlying collection that is bound to the DGV

You may need to have an ALL collection and filter that into a new collection to support the clear filter functionality.
   
   
v3
Comments
v_tigra 10-Jan-14 2:55am
   
I already tried this one:

BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = dataGridView1.Columns[1].HeaderText.ToString() + " LIKE '%" + searchTextBox.Text + "%'";
dataGridView1.DataSource = bs;

but it doesn't show anything..
hassanshaygan 24-Oct-21 10:42am
   
ok
Naz_Firdouse 10-Jan-14 3:04am
   
what kind of datasource you are binding?
because
MSDN documentation[^] says:

Only underlying lists that implement the IBindingListView interface support filtering.
v_tigra 10-Jan-14 3:16am
   
I just load all entries from my database table into the datagridview control
Naz_Firdouse 10-Jan-14 3:41am
   
try using Rowfilter as specified in the second link
hi try this one

C#
private void TextBox_TextChanged(object sender, EventArgs e)
{
    (DataGridView.DataSource as DataTable).DefaultView.RowFilter =
    string.Format("Name LIKE '{0}%' OR Name LIKE '% {0}%'", TextBox.Text);
}
   
Comments
v_tigra 10-Jan-14 8:07am
   
thank you so much
   
Comments
v_tigra 10-Jan-14 3:15am
   
I already tried this but nothing happens..
   
Comments
v_tigra 10-Jan-14 3:22am
   
read solution 3
private void searchsimpleButton_Click(object sender, EventArgs e)
{
SqlConnection con;
string st = @"Data Source=User-PC\MYSQLSERVER;Initial Catalog=officeproject;Integrated Security=True";
con = new SqlConnection(st);
con.Open();
string s = @"select name,nrc,phone number from member where ([name] LIKE '%' + @name)";

SqlCommand cmd = new SqlCommand(s, con);
cmd.Parameters.AddWithValue("@name", textBox1.Text);

SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "member");
gridControl1.DataSource = ds;
gridControl1.DataMember = "member";


con.Close();
}
   
Comments
v_tigra 10-Jan-14 4:09am
   
is there another possibility to make this without open the connection like this? - databind?
eieisandi 11-Jan-14 5:07am
   
Are you take out data from database table.if using database table this coding is ok.
C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dgv1.DataSource;
            bs.Filter = "[ColumnName1] like '%" + textBox1.Text + "%' " +
                "OR [ColumnName2] like '%" + textBox1.Text + "%'" +
                "OR [ColumnName3] like '%" + textBox1.Text + "%'" +
                "OR [ColumnName4] like '%" + textBox1.Text + "%'";
            dgv1.DataSource = bs; 
        }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900