FilteredBindingList & GridView

FilteredBindingList & GridView

Old forum URL: forums.lhotka.net/forums/t/3295.aspx


dantruj posted on Monday, July 30, 2007

I downloaded the csla 2.1 handbook and read about the filtered list and sorting list classes and know how to use them.  The problem I'm having is figuring how to use the filtered binding list with a gridview.  In the clsaDataSource.SelectObject event I can create a sortedbindinglist and sort easily, but how do I get filtering to work.  I know I can use the same sortedlist to plug into the filteredbindng list and vice-versa but what I'm really after is implementation.

So here's my problem:

I have a webform with a gridview displaying contacts from contactsList (read-only collection).  The gridview is setup for sorting via the cslaDataSource.SelectObject event.  I have a text box that accepts a contacts lastname and a button that is suppose to execute the filter.  I am wondering how does the gridview get bound to the filtered list?  In the cslaDataSource.SelectObject event there is a e.business object that I can set but how do I get the filter working?

The code below:  notice for the sorting e.business object is used to populate the grid, but how do I do the same in the lnkBtnFilter_Click event?  I got it to work by doing the binding myself, but I know there has to be a better practices way.

protected void lnkBtnFilter_Click(object sender, EventArgs e)

{

   ContactList contactsList = GetContactsList(false);

   FilteredBindingList<ContactInfo> contactsFilteredList = new FilteredBindingList<ContactInfo>(contactsList);

   contactsFilteredList.ApplyFilter("Name", txtFilterLN.Text.Trim());

   ContactsDataGrid.DataSource = null;

   ContactsDataGrid.DataSourceID = null;

   ContactsDataGrid.DataSource = contactsFilteredList;

   ContactsDataGrid.DataBind();

}

protected void ContactsListDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)

{

   ContactList contactsList = GetContactsList(false);

   SortedBindingList<ContactInfo> contactsSortedList = new SortedBindingList<ContactInfo>(contactsList);

   contactsSortedList.ApplySort(GridSortExpression, ListDirection);

   e.BusinessObject = contactsSortedList;

}

 

Thanks for the help in advance.

DT

 

RockfordLhotka replied on Tuesday, July 31, 2007

FilteredBindingList works just like SortedBindingList, except that it applies a filter instead of a sort.

This means that you'd use FilteredBindingList in the SelectObject event handler in place of (or in addition to) SortedBindingList, and you'd set e.BusinessObject equal to the filtered list.

The DataBind() call in your button click handler triggers the data binding refresh, which ultimately raises the SelectObject event. So it is up to you to figure out how to provide your filter parameter to the SelectObject event handler, but in your example a form-level field would work for this purpose.

If you look in the 2.1 Handbook you'll also see how you can use information automatically provided by the SelectObjectArgs parameter, and that may be helpful too.

dantruj replied on Tuesday, July 31, 2007

Thank you! that's what I needed, I feel so dumb now.  I didn't think about just calling databind()

Copyright (c) Marimer LLC