This is a migrated thread and some comments may be shown as answers.

Refresh filter when typing

5 Answers 237 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 04 Jun 2014, 08:55 AM
Hi,

I'm searching for a solution to filter the source immediately when the user types in the filter criteria. This is my declaration in my xaml:

<telerik:RadGridView Name="grvKanten" AutoGenerateColumns="False" IsReadOnly="True" CanUserReorderColumns="True" FilteringMode="FilterRow"
                     FilterOperatorsLoading="grvKanten_FilterOperatorsLoading" >
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Name="colKantenname" DataMemberBinding="{Binding Kantenname}" Header="Kantenname" MinWidth="150"/>
        <telerik:GridViewDataColumn Name="colKantenbreitenname" DataMemberBinding="{Binding Kantenbreitenname}" Header="Kantenbreiten-Name" MinWidth="150"/>
        <telerik:GridViewDataColumn Name="colLagerplatz" DataMemberBinding="{Binding Lagerplatzname}" Header="Lagerplatz" MinWidth="150"/>
        <telerik:GridViewDataColumn Name="colLaenge" DataMemberBinding="{Binding Laenge}" Header="Länge" MinWidth="100"/>
        <telerik:GridViewDataColumn Name="colDekor" DataMemberBinding="{Binding Dekor}" Header="Dekor" MinWidth="100"/>
        <telerik:GridViewDataColumn Name="colBreie" DataMemberBinding="{Binding Breite}" Header="Kantenbreite" MinWidth="100"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

In my codebehind I delete some filter operators and set a default operator for each column when the FilterOperatorsLoading event is fired:
Private Sub grvKanten_FilterOperatorsLoading(sender As System.Object, e As Telerik.Windows.Controls.GridView.FilterOperatorsLoadingEventArgs)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.EndsWith)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsContainedIn)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsEmpty)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsEqualTo)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsGreaterThanOrEqualTo)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsLessThanOrEqualTo)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsNotEmpty)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsNotEqualTo)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsNotNull)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsNull)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.StartsWith)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsLessThan)
      e.AvailableOperators.Remove(Telerik.Windows.Data.FilterOperator.IsGreaterThan)
      e.DefaultOperator1 = Telerik.Windows.Data.FilterOperator.Contains
End Sub

So, if the user types in some filter criterias, I want the underlying source to be immediately refreshed every time the criteria changes without pressing the return button.

I tryed to set IsFilteringDeferred to false and true for some columns, but that doesn't seem to make a difference. The behavior is always the same: I have to press return to refresh the source.

Kind regards

5 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 04 Jun 2014, 12:50 PM
Hi,

I attached a demo project illustrating how you can filter when user types.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Daniel
Top achievements
Rank 1
answered on 05 Jun 2014, 08:21 AM
Hi Didie,

thanks for this nice solution. It works great for me after converting to vb:

Private Sub grvKanten_FieldFilterEditorCreated(sender As Object, e As EditorCreatedEventArgs)
  Dim stringFilterEditor As StringFilterEditor = TryCast(e.Editor, StringFilterEditor)
  If Not stringFilterEditor Is Nothing Then
    AddHandler e.Editor.Loaded, Sub(s1, e1)
                                  Dim textBox__1 = e.Editor.ChildrenOfType(Of TextBox)().[Single]()
                                  AddHandler textBox__1.TextChanged, Sub(s2, e2)
                                                                       textBox__1.GetBindingExpression(TextBox.TextProperty).UpdateSource()
                                                                     End Sub
                                End Sub
  End If
End Sub

I just added some lines of code to make the example also work with types other than string:

Dim tb As TextBox = TryCast(e.Editor, TextBox)
If Not tb Is Nothing Then
  AddHandler tb.TextChanged, Sub(s2, e2)
                               tb.GetBindingExpression(TextBox.TextProperty).UpdateSource()
                             End Sub
End If

Kind regards
0
Dimitrina
Telerik team
answered on 05 Jun 2014, 09:07 AM
Hi,

Thank you for sharing this additional code.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Risa
Top achievements
Rank 1
answered on 12 Jan 2017, 06:03 PM
The above solution works for me for all text fields. However, it does not deal with numericfields. Is there a way to filter as the user is typing in numbers in a number field?
0
Risa
Top achievements
Rank 1
answered on 12 Jan 2017, 06:18 PM

I believe I found the answer you my question. I've expanded the SetFilterAutoUpdate method to have an else statement, for non-text fields. If it's a numeric field, e.Editor will be a simple TextBox, and we can then subscribe to its TextChanged Event to update the filters immediately.

public static void SetFilterAutoUpdate(RadGridView grid, object sender, EditorCreatedEventArgs e)
    {
      if ((e.Editor is StringFilterEditor)) {
        e.Editor.Loaded += (RoutedEventHandler) ((s1, e1) => {
          TextBox textBox = e.Editor.ChildrenOfType<TextBox>().Single<TextBox>();
          textBox.TextChanged += (TextChangedEventHandler) ((s2, e2) => textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource());
        });
       
      }
      else {
        TextBox textBox = e.Editor as TextBox;
        if (textBox != null) {
          textBox.TextChanged += (TextChangedEventHandler)((s2, e2) => textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource());
        }

      }
    }

Tags
GridView
Asked by
Daniel
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Daniel
Top achievements
Rank 1
Risa
Top achievements
Rank 1
Share this question
or