GridView FilterList/BindingList Examples?

GridView FilterList/BindingList Examples?

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


dantruj posted on Monday, July 30, 2007

Does anyone have an example of a GridView databinding to a FilteredBindingList?

dcleven replied on Friday, September 07, 2007

Here is some code that refreshes a DataGridView with a filtered/sorted list. It assumes you have a DataGridView object and you simply need to assign a filtered list to it.

Sorry about the formatting/double space... I can't figure out how to get code to look nice in here!

/// <summary>

/// RefreshGrid gets an InventoryAssignmentList and filters/sorts the list

/// then sets the grid DataSource.

/// </summary>

private void RefreshGrid()

{

   InventoryAssignmentList.InvalidateCache();

   // get the list of all InventoryAssignment objects

   InventoryAssignmentList list = InventoryAssignmentList.GetList();

   // create the filter object

   Csla.FilteredBindingList<InventoryAssignment> filteredList =

      new Csla.FilteredBindingList<InventoryAssignment>(list);

   // get the categoryID we want to filter on

   int catID = GetSelectedCategoryIDInTree();

   // apply the filter based on the property CategoryId

   if (catID > 0)

      filteredList.ApplyFilter("CategoryId", catID);

   // create a SortedBindingList with our filteredList

   Csla.SortedBindingList<InventoryAssignment> sortedList =

      new Csla.SortedBindingList<InventoryAssignment>(filteredList);

   // apply the sort based on TypeName property

   sortedList.ApplySort("TypeName", ListSortDirection.Ascending);

   // apply the filtered/sorted list to the DataGridView.DataSource

   inventoryAssignmentListBindingSource.DataSource = sortedList;

   inventoryAssignmentListBindingSource.ResetBindings(false);

}

Copyright (c) Marimer LLC