This is a migrated thread and some comments may be shown as answers.
RadGridView custom filter operator
5 Answers 164 Views
This is a migrated thread and some comments may be shown as answers.
Joar
Top achievements
Rank 1
Joar asked on 28 Mar 2019, 10:44 PM

Hi, I have a RadGridView where I want to add a custom filter operator to the alternatives that is already existing.

If a "-" is between two strings (like "03-15"), would like it to look something like this:

           

 

I have all the filter logic, but I can't figure how to add a new filter operator. Is it possible? Then, how to do it?

If it is not possible, then I would want to make a custom filtering row that has only the custom filtering method, how should I then create a filter row that has only the filter functionality that I want?

If I could use the filter row as I wanted without the existing filter logic, then I could use the text in the filter text box to make a custom filter, or try to build a composite filter with many BeginsWith that covers the desired range.

So what I need is a custom filter operator (would be the best in my mind), or a way to customize the filter row to do only the operation I need.

Regards,
Joar

 

5 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 Mar 2019, 01:23 PM
Hello, Joar,  

RadGridView offers a flexible public API for custom filtering which allows you to fully control the filtering logic. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/controls/gridview/filtering/custom-filtering

You can also refer to our Demo application >> GridView >> Filtering >> Custom Filtering example which is quite useful on this topic.

As to the question about adding a custom filter option in the filter menu, you can handle the ContextMenuOpening event and add a new RadMenuItem. When the item is clicked, you can add the desired FilterDescriptor programmatically. A sample code snippet is illustrated below. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best: 

public RadForm1()
{
    InitializeComponent();
 
    this.radGridView1.Columns.Add("Number");
    this.radGridView1.Columns.Add("TextColumn");
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
    this.radGridView1.EnableFiltering = true;
    this.radGridView1.ShowHeaderCellButtons = true;
     
    this.radGridView1.Rows.Add(1, "");
    this.radGridView1.Rows.Add(2, " ");
    this.radGridView1.Rows.Add(3, null);
    this.radGridView1.Rows.Add(4, "test");
 
    this.radGridView1.ContextMenuOpening += radGridView1_ContextMenuOpening;
}
 
private void radGridView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e)
{
    GridFilterCellElement filterCell = e.ContextMenuProvider as GridFilterCellElement;
    if (filterCell != null && filterCell.ColumnInfo is GridViewTextBoxColumn)
    {
        RadMenuItem emptyItem = new RadMenuItem("IsEmpty");
        emptyItem.Tag = filterCell.ColumnInfo.Name;
        emptyItem.Click += emptyItem_Click;
        e.ContextMenu.Items.Add(emptyItem);
    }
}
 
private void emptyItem_Click(object sender, EventArgs e)
{
    RadMenuItem item = sender as RadMenuItem;
    string propertyName = item.Tag + "";
      
    FilterDescriptor fd = new FilterDescriptor();
    fd.PropertyName = propertyName;
    fd.Operator = FilterOperator.IsEqualTo;
    fd.IsFilterEditor = true;
    fd.Value = "";
    this.radGridView1.FilterDescriptors.Add(fd);
}

Note that you can also use the Custom option in the filter menu to build a more complex filter expression.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
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.
0
Joar
Top achievements
Rank 1
answered on 05 Apr 2019, 10:37 AM

This was almost all that I needed. In addition to the described solution from you, I subscribed to the tasksGridView.FilterExpressionChanged event to empty out the default filter operator stuff, and updated the custom filtering that is used in tasksGridView.CustomFiltering event with info from the tasksGridView.FilterChanging event. When wanting to use the default filters, I just dont empty the 

 

Thank you for the support!

Regard,

Joar

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 Apr 2019, 08:27 AM
Hello, Joar,   

I am glad that the suggested solution was suitable for your case. However, have in mind that if the custom filtering is enabled it is not necessary to remove any FilterDescriptor from the FilterDescriptors collection. The custom filtering behavior has a higher priority than the default filter and although the filtering UI is indicated in the same way, the CustomFiltering event controls how the rows will be filtered. If you set the Handled argument to true, you need to specify whether the row will be visible or not by the Visible argument in the GridViewCustomFilteringEventArgs.

In order to determine more easily whether a row is visible or not in the CustomFiltering event when the grid is filtered by several columns, you can use the MasterTemplate.DataView.FilterEvaluate method. This method accepts a FilterDescriptor and a row as parameters and returns a boolean value indicating whether the row will be visible or not according to the passed FilterDescriptor. Thus, you can combine the custom logic for a certain column and evaluate the default one for the rest of the column's filters.

Please refer to the following help article which demonstrates how the custom filtering works: https://docs.telerik.com/devtools/winforms/controls/gridview/filtering/custom-filtering

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
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.
0
Gh Reza
Top achievements
Rank 1
answered on 11 Apr 2020, 04:37 PM

Hi Dear !

I have a radGridView that have an CustomFilter in design time .

But at runtime when user add new row or change existing rows that not match with filter. Filter event does not fired and radGridView rows not filterd.

please see following code snippet :

    this.radGridView1.MasterTemplate.EnableCustomFiltering = true;
      this.radGridView1.CustomFiltering +=
                new GridViewCustomFilteringEventHandler(radGridView_CustomFiltering);

 private void radGridView1_CustomFiltering(object sender, GridViewCustomFilteringEventArgs e)
        {
            e.Visible = (EntityState)e.Row.Cells["EntityState"].Value != EntityState.Deleted;
        }

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Apr 2020, 12:23 PM

Hello, Reza,

In the CustomFiltering event there is an argument which is called Handled. It defines whether the row is processed by the custom algorithm or by the applied filter descriptors. If it is set to true, the Visible argument will take effect.

Additional information about how the custom filtering works is available in the following help article: https://docs.telerik.com/devtools/winforms/controls/gridview/filtering/custom-filtering 

You can also have a look at our Demo application >> GridView >> Filtering >> Custom Filtering example.

I hope this information helps. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Asked by
Joar
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Joar
Top achievements
Rank 1
Gh Reza
Top achievements
Rank 1
Share this question
or