Tutorial :Programatically removing a header context menu filter from RadGrid



Question:

I've got a repeater I've bound to column names that are filtered, as they're applied (using sessions currently, I might change this to loop through the columns looking for filters now that I really know how the grid works).

I've got a button next to each filtered column name that is to remove the filter from the RadGrid.

        <asp:Repeater ID="repCorpFilters" runat="server" OnItemCommand="repFilters_ItemCommand">              <HeaderTemplate>                  Current Filters:              </HeaderTemplate>              <ItemTemplate>                  <asp:ImageButton runat="server" CommandName="removefilter" CommandArgument="corp" ImageUrl="../images/Delete.gif" />                  <asp:literal ID="litFilter" runat="server" Text='<%#Container.DataItem() %>' />              </ItemTemplate>              <SeparatorTemplate>                  ,               </SeparatorTemplate>          </asp:Repeater>      Protected Sub repFilters_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)      Select Case e.CommandName          Case "removefilter"              BindGrid(DirectCast([Enum].Parse(GetType(products), e.CommandArgument), products), Nothing, _                       CType(e.Item.FindControl("litFilter"), Literal).Text)      End Select  End Sub      Private Sub BindGrid(ByVal prod As products, Optional ByVal cusipFilter As String = Nothing, Optional ByVal strRemoveFilter As String = Nothing)      Dim dvCorp As DataView = CachedPartialCorp()      If Not IsNothing(cusipFilter) Then          isCusipFiltered = True          dvCorp.RowFilter = "CUSIP IN " & cusipFilter      End If        If Not IsNothing(strRemoveFilter) Then          Dim filters As List(Of String) = RemoveFilter("partialCorpFilters", strRemoveFilter)            If filters.Count = 0 Then              repCorpFilters.Visible = False          Else              repCorpFilters.DataSource = filters              repCorpFilters.DataBind()          End If            For Each gc As GridColumn In dtgCorp.MasterTableView.Columns              With gc                  If .IsBoundToFieldName(strRemoveFilter) Then                      .CurrentFilterFunction = GridKnownFunction.NoFilter                      .CurrentFilterValue = ""                      .AndCurrentFilterFunction = GridKnownFunction.NoFilter                      .AndCurrentFilterValue = ""                  End If              End With          Next      End If        dtgCorp.DataSource = dvCorp      dtgCorp.DataBind()  End Sub  

Everything works fine except the actual removal of the filter from the RadGrid. Where I loop through the columns and find the correct column with the filter I'm trying to remove, I reset the filter drop downs and values, that works great! I can right click the header item, and the filter shows not filtered. But the data stays filtered! How do I tell the RadGrid to re-evaluate its new filtervalue based on the selected filters?

The juicy bit of the code I feel is here:

        For Each gc As GridColumn In dtgCorp.MasterTableView.Columns              With gc                  If .IsBoundToFieldName(strRemoveFilter) Then                      .CurrentFilterFunction = GridKnownFunction.NoFilter                      .CurrentFilterValue = ""                      .AndCurrentFilterFunction = GridKnownFunction.NoFilter                      .AndCurrentFilterValue = ""                  End If              End With          Next      End If        dtgCorp.DataSource = dvCorp      dtgCorp.DataBind()  


Solution:1

        For Each gc As GridColumn In dtg.MasterTableView.Columns              With gc                  If .IsBoundToFieldName(strRemoveFilter) Then                      dtg.MasterTableView.GetItems(GridItemType.FilteringItem)(0).FireCommandEvent(RadGrid.HeaderContextMenuFilterCommandName, _                          New Triplet(strRemoveFilter, New Pair("NoFilter", ""), New Pair("NoFilter", "")))                  End If              End With          Next  

This is the code that finally did it :)


Solution:2

Have you tried using NeedDataSource binding for the grid instead of binding with DataBind() calls? Just invoke the Rebind() method of the grid in this case to clear the filter values. Also see how the filters are cleared on this demo.

Dick


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »