• Home
  • About
  • Resume

Showing ALL filters in UltraWebGrid with paging

By , 04/12/2010 1:39 PM

If you’re using Infragistics classic UltraWebGrid with LoadOnDemand not set and paging enabled, getting column filters to work can be tricky. By default clicking on Filter icon will display column data from current page only, ignoring other pages. To make it work you have to take matter in your own hands – populate filter data in code.

The best place to do it is in InitializeLayout event. There you can loop thru all the columns, calling function to populate column filters:

Protected Sub xMyGrid_InitializeLayout(ByVal sender As Object, ByVal e As LayoutEventArgs) Handles xMyGrid.InitializeLayout
    For Each ugColumn As UltraGridColumn In e.Layout.Grid.Columns
        GatherFilterDataForColumn(ugColumn)
    Next
End Sub


And now the function that populates filters for the column:

Sub GatherFilterDataForColumn(ByVal i_oColumn As UltraGridColumn)
    Dim dtData As DataTable = Session("Data")

    Dim aFilters = From oRow In dtData _
        Where oRow(i_oColumn.BaseColumnName) IsNot DBNull.Value _
        Select sFilter = _
        oRow.Field(Of String)(i_oColumn.BaseColumnName).Replace("""", "\""") _
        Distinct Order By sFilter

    i_oColumn.GatherFilterData = Shared.DefaultableBoolean.False
    i_oColumn.FilterCollectionValues.Clear()

    For Each sFilter In aFilters
        i_oColumn.FilterCollectionValues.Add(sFilter, sFilter)
    Next

End Sub

The function accepts an UltraGridColumn as a parameter, then (Line 2) retrieves data associated with the grid (in this example from previously assigned session variable, but there could be different source). Lines 4-8 create a LINQ query to retrieve distinct values for given column. There’re other ways of achieving the same result, but if you have .NET 3.5 installed – this is the shortest and the fastest. Let’s take the query apart:

Line 5 makes sure that NULL values are filtered out, otherwise query may throw an error
Line 7 selects a specific cell from current row based on column name. Also note the Replace statement – it replaces a double quote character with its JavaScript representation. Otherwise you could get a weird JavaScript error from deep inside of Infragistics JavaScript code.
Line 8 makes sure that results are unique and ordered. Speaking of “DISTINCT”. If you get error message “Definition of method ‘Distinct’ is not available in this context” make sure you imported System.Linq namespace. If not simple add Imports System.Linq at the beginning of your code

This is pretty much it. The rest of the code tells column not to gather filter data automatically and populates it looping thru query results.

To make sure actual filtering works correctly you also have to capture RowFilterApplied grid event and in there reset Pager’s CurrentPageIndex to 1 and rebind the grid to data source.

Leave a Reply

Panorama Theme by Themocracy