This is a migrated thread and some comments may be shown as answers.
How to partuially clear Grid filter expression
1 Answer 12 Views
This is a migrated thread and some comments may be shown as answers.
Galina
Top achievements
Rank 1
Galina asked on 17 Aug 2018, 09:05 PM

I have a grid with filter templates looking like this:

 <FilterTemplate>
                        <telerik:RadComboBox ID="ddCategory3" runat="server" DropDownAutoWidth="Enabled"
                            AppendDataBoundItems="true" CausesValidation="false"
                            DataTextField="Category" DataValueField="Category"
                           DataSourceID="dsCategory" SelectedValue='<%#  TryCast(Container, GridItem).OwnerTableView.GetColumn("Category").CurrentFilterValue %>' OnClientSelectedIndexChanged="Category">
                            <Items>
                                <telerik:RadComboBoxItem Text="All" />
                            </Items>
                        </telerik:RadComboBox>
                        <telerik:RadScriptBlock ID="RadScriptBlockCategory" runat="server">
                            <script type="text/javascript">
                                function Category(sender, args) {
                                    var tableView = $find("<%# TryCast(Container, GridItem).OwnerTableView.ClientID %>");
                                    tableView.filter("Category", args.get_item().get_value(), "EqualTo");
                                }
                            </script>
                        </telerik:RadScriptBlock>
                    </FilterTemplate>

If I have just one item with value "ABC" in column "Category"  and I filter my grid by that column and value, I will receive just one item. My grid has delete functionality. If I try to delete this item, I am getting error "Selection is out of range." after the item is deleted. I figured out, that it is because my combobox data source contains distinct values for this column. When I delete last Item with value "ABC" combobox rebinds and doesn't have item ABC anymore, but current filter value is still ABC, so it fails.

I try to solve it by removing  SelectedValue='<%#  TryCast(Container, GridItem).OwnerTableView.GetColumn("Category").CurrentFilterValue %>'  and adding ondatabound function, where I check if current value exists  in item collection. I exists, I  set set selected value as current filter value.  Otherwise I set selection to ALL  and clear the filter . It is all works fine if I only  have filter for that one column. But I have multiple columns and ideally I would prefer not to clear  FilterExpression completely, but just remove a portion that represents this column  filter. It there any easy solution for that other then parsing the Filter expression manually?  Or may be my approach to solving the issue is not the best one? I am open to suggestions.

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 23 Aug 2018, 11:54 AM
Hi Galina,

Instead of the markup, try setting the selected value in the code behind.  There you can apply additional logic and prevent such exceptions. For that, you can use the PreRender event of the combobox.

Attached the PreRender event to the combobox:
<telerik:RadComboBox ID="ddCategory3" runat="server" OnPreRender="ddCategory3_PreRender">
</telerik:RadComboBox>

ddCategory3_PreRender eventhandler:
Protected Sub ddCategory3_PreRender(sender As Object, e As EventArgs)
    ' reference the combobox
    Dim combo As RadComboBox = TryCast(sender, RadComboBox)
 
    ' get reference to the column with combo filtering
    Dim col As GridColumn = RadGrid1.MasterTableView.GetColumn("ShipName")
 
    ' condition to check whether filtering is applied
    If Not String.IsNullOrEmpty(col.CurrentFilterValue) Then
        ' if filter is applied, look for an item in the combo and get a reference to it
        Dim comboItem As RadComboBoxItem = combo.FindItemByValue(col.CurrentFilterValue)
 
        ' if item exists
        If comboItem IsNot Nothing Then
            ' set the item selected
            combo.FindItemByValue(col.CurrentFilterValue).Selected = True
        Else
            ' if the item does not exist, create a new one with the same text as the current filter value
            comboItem = New RadComboBoxItem(col.CurrentFilterValue)
 
            ' add the item to the combo
            combo.Items.Add(comboItem)
            'set the new item selected
            comboItem.Selected = True
        End If
    End If
End Sub

Please check out the attached sample to see this in action.

I hope this will prove helpful.

Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Galina
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or