Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi for filtering DataGridView i write this code but not work why ?

C#
BindingSource bs = new BindingSource();
                   bs.DataSource = dgvSPage.DataSource;
       private void textBox1_TextChanged(object sender, EventArgs e)
       {
           try
           {

               if(dgvSPage.Rows.Count>0)
               {

                  bs.Filter =  string.Format("Id = {0} ", txtsearchId.Text);
                  dgvSPage.DataSource = bs;
               }

               if (txtsearchId.Text == string.Empty)
               {
                   bs.RemoveFilter();
                   bs.Filter = null;
                   dgvSPage.DataSource = bs;
               }
           }
           catch (Exception er)
           {
               MessageBox.Show(er.Message + "\n" + er.Source + "\n" + er.StackTrace + "\n" + er.TargetSite);
               throw;

           }
       }


How filter datagridview??
Posted

1 solution

Try writing your filter this way:

C#
bs.Filter=String.Format("Id LIKE '%{0}%'", txtsearchId.Text):


(added '% %'). if this doesn't work:

The MSDN Documentation https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter(VS.90).aspx[^] says:

Only underlying lists that implement the IBindingListView interface support filtering.


so maybe it's a problem with your datasource ?
 
Share this answer
 
v2
Comments
NorouziFar 15-Apr-15 4:13am    
Not Work
Florian Braun 15-Apr-15 4:21am    
i changed something. you can try again and should read the MSDN Documentation. Maybe its your Datasource that can't be filtered

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