以编程方式从 RadGrid 中删除标题上下文菜单过滤器
我有一个中继器,在应用时绑定到已过滤的列名称(当前使用会话,我可能会更改它以循环遍历列以查找过滤器,现在我真的知道网格是如何工作的)。
每个过滤列名称旁边都有一个按钮,用于从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是最终完成的代码:)
This is the code that finally did it :)
您是否尝试过对网格使用 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