Print Page | Close Window

Filtering Rows in DataGridView using BindingSource

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=404
Printed Date: 11-Jun-2026 at 9:05pm


Topic: Filtering Rows in DataGridView using BindingSource
Posted By: Tich
Subject: Filtering Rows in DataGridView using BindingSource
Date Posted: 04-Sep-2007 at 7:25am

I have a DataGridView hooked up to a  DataGridViewBindingManager which has a BindingSource when I set the filter property of the BindingSource the rows displayed in the DataGridView are not filtered

help !   Unhappy




Replies:
Posted By: GregD
Date Posted: 04-Sep-2007 at 5:48pm
Tich:
 
You can't use the Filter property of the BindingSource, as there is no syntax defined for it.  It's one of those raw .NET facilities that require custom implementation to be usable.
 
Instead, assign an EntityList<T> to the BindingSource as its DataSource,
then do a ReplaceRange() on the EntityList whenever you need to filter the rows. E.g.,
 
EntityList<Employee> mEmployees = new EntityList<Employee>();
mEmployeesBindingSource.Datasource = mEmployees;
RdbQuery anRdbQuery = new RdbQuery(typeof(Employee));
anRdbQuery.AddClause(BirthDateEntityColumn, EntityQueryOp.GT, new DateTime(1950, 1, 1));
mEmployees.ReplaceRange(PersistenceManager.DefaultManager.GetEntities<Employee>(anRdbQuery));
 
 
Greg Dunn
IdeaBlade

 


Posted By: Tich
Date Posted: 05-Sep-2007 at 6:48am

Thanks I will try that approach, but the code I have works if  I only use a DataGridView and a BindingSource. Trouble comes when I use a DataGridView BindingManager hooked up to a BindingSource and get the data from an entity list.  

The code below is an edited extract from the class frmSearchGrid() which allows me to search any DataGridView because the code generates the search parameter (strFilter) dynamically.

//constructor
frmSearchGrid( DataGridView _DataGrid, BindingSource _BS)  
…..

SearchGrid ()
{
   ….

            // cboColumn is a combo box that lists all the visible columns in the DataGridView

 String strFilter = "[" +      cboColumn.SelectedItem.ToString() + "]" + " = " + "'" +    txtSearch.Text.ToString() + "'";

                   _BS.Filter = strFilter;

             if ( _DataGrid.Rows.Count == 0)

             {

// search item not found 
       _BS.RemoveFilter();

                        MsgBox.Show(" Search item not found ");

                        txtSearch.Focus();

                        return;

              }

            else

            {  

                        // search item found

 this.Close();

                                return;

            }
  ….

}

 




Print Page | Close Window