Completed
Last Updated: 22 Dec 2017 14:00 by ADMIN
ADMIN
Hristo
Created on: 02 Jun 2017 09:35
Category: GridView
Type: Bug Report
2
FIX. RadGridView - incorrect excel-like filtering in the RadListFilterPopup of DateTime values
How to reproduce: 
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.radGridView1.DataSource = this.GetData();
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.EnableFiltering = true;
        this.radGridView1.ShowHeaderCellButtons = true;

        this.radGridView1.FilterPopupRequired += RadGridView1_FilterPopupRequired;

        GridViewDateTimeColumn date = this.radGridView1.Columns["Date"] as GridViewDateTimeColumn;
        date.DataType = typeof(DateTime);
        date.FilteringMode = GridViewTimeFilteringMode.Date;
    }

    private void RadGridView1_FilterPopupRequired(object sender, Telerik.WinControls.UI.FilterPopupRequiredEventArgs e)
    {
        if (e.Column.GetType().Name == "GridViewDateTimeColumn")
        {
            RadListFilterPopup popup = new RadListFilterPopup(e.Column, true);
            e.FilterPopup = popup;
        }
    }

    private object GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Columns.Add("Bool", typeof(bool));

        DateTime date = DateTime.Now;
        for (int i = 0; i < 1500; i++)
        {
            dt.Rows.Add(i, "Name " + i, date.AddDays(i), i % 2 == 0);
        }

        return dt;
    }

Workaround: custom RadListFilterPopup

public class MyRadListFilterPopup : RadListFilterPopup
{
    public MyRadListFilterPopup(GridViewDataColumn dataColumn, bool groupedDateValues)
        : base(dataColumn, groupedDateValues)
    { }

    protected override void OnButtonOkClick(EventArgs e)
    {
        FilterOperator filterOperator = FilterOperator.IsEqualTo;

        IRadListFilterElement listFilterElement = typeof(RadListFilterPopup).GetField("listFilterElement", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).
            GetValue(this) as IRadListFilterElement;

        switch (listFilterElement.SelectedMode)
        {
            case ListFilterSelectedMode.All:
                filterOperator = FilterOperator.None;
                break;
            case ListFilterSelectedMode.Null:
                filterOperator = FilterOperator.IsNull;
                break;
            case ListFilterSelectedMode.NotNull:
                filterOperator = FilterOperator.IsNotNull;
                break;
        }

        if (filterOperator != FilterOperator.IsEqualTo)
        {
            SetFilterOperator(filterOperator);
            this.ClosePopup(RadPopupCloseReason.CloseCalled);
        }
        else
        {
            CompositeFilterDescriptor compositeFilterDescriptor = new CompositeFilterDescriptor();
            compositeFilterDescriptor.PropertyName = base.DataColumn.Name;
            compositeFilterDescriptor.LogicalOperator = FilterLogicalOperator.Or;

            foreach (DictionaryEntry entry in listFilterElement.SelectedValues)
            {
                foreach (object value in (ArrayList)entry.Value)
                {
                    FilterDescriptor descriptor;
                    if (base.DataColumn is GridViewDateTimeColumn || base.DataColumn.DataType == typeof(DateTime) ||
                        base.DataColumn.DataType == typeof(DateTime?))
                    {
                        descriptor = new DateFilterDescriptor(base.DataColumn.Name, FilterOperator.IsEqualTo, (DateTime?)value, false);
                    }
                    else
                    {
                        descriptor = new FilterDescriptor(base.DataColumn.Name, FilterOperator.IsEqualTo, value);
                    }
                    compositeFilterDescriptor.FilterDescriptors.Add(descriptor);
                }
            }

            base.FilterDescriptor = compositeFilterDescriptor;
            OnFilterConfirmed();
        }
    }
}
Attached Files:
(Total attached files size should be smaller than 20mb. Allowed extensions: .zip, .rar, .jpg, .png, .gif)
0 comments