以编程方式从 RadGrid 中删除标题上下文菜单过滤器

发布于 2024-09-09 10:02:58 字数 3074 浏览 3 评论 0原文

我有一个中继器,在应用时绑定到已过滤的列名称(当前使用会话,我可能会更改它以循环遍历列以查找过滤器,现在我真的知道网格是如何工作的)。

每个过滤列名称旁边都有一个按钮,用于从 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

除了从 RadGrid 中实际移除过滤器之外,一切工作正常。在我循环遍历列并找到带有我要删除的过滤器的正确列的地方,我重置了过滤器下拉列表和值,效果非常好!我可以右键单击标题项,过滤器显示未过滤。但数据仍然被过滤!如何告诉 RadGrid 根据所选过滤器重新评估其新过滤器值?

我觉得最有趣的代码在这里:

        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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硪扪都還晓 2024-09-16 10:02:58
        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

这是最终完成的代码:)

        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 :)

心头的小情儿 2024-09-16 10:02:58

您是否尝试过对网格使用 NeedDataSource 绑定而不是与 DataBind() 调用绑定?在这种情况下,只需调用网格的 Rebind() 方法即可清除过滤器值。另请参阅 上如何清除过滤器这个演示

迪克

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文