Getting List<FilterElement> of column without opening filter control

Is it possible to get the list of all FilterElements for a column? We have a requirement to have couple of grids with only one showing the filtering option, but when applying filter it  should apply to both. Is there any way we can get the List of FilterElements from a grid programmatically?

1 Reply

DY Deivaselvan Y Syncfusion Team August 15, 2018 01:46 PM UTC

Hi Jimmy,

We do not have direct API to get the FilterElement and it can be get from the FilterItemsPopulated event only because we create FilterElement OnDemand for every time while click on the Filter Icon and we are not holding that in live memory, so it is not possible to get this programmatically. However, we have prepared an example to create FilterElement for the same programmatically using the below code example for the unique values of the OrderID column. Please try running the attached sample for the same and let us know if this helps you.  
    private void button_Click_1(object sender, RoutedEventArgs e)  
    {  
        var filteredResult = this.sfdatagrid.View.Records.Select(recordentry => recordentry.Data);  
        var viewObjects = filteredResult.Select(x => (x asOrderDetails).OrderID).ToList<object>().Distinct<object>();  
        var column = this.sfdatagrid.Columns.FirstOrDefault(x => x.MappingName =="OrderID");  
        //List of FilterElement 
        List<FilterElement> distinctCollection = viewObjects.Select(item =>  
                                                        new FilterElement  
                                                        {  
                                                            DisplayText = item.ToString(),  
                                                            IsSelected = column.FilteredFrom != FilteredFrom.AdvancedFilter,  
                                                            ActualValue = item,  
                                                        }).ToList();  
  
        distinctCollection.Sort(new FilterElementAscendingOrder());  
  
   }  
  
  
internal class FilterElementAscendingOrder : IComparer<IFilterElement>  
{  
    public int Compare(IFilterElement x, IFilterElement y)  
    {  
        if (x.ActualValue == null || y.ActualValue == null || x.ActualValue is DBNull || y.ActualValue is DBNull)  
        {  
            if ((x.ActualValue == null && y.ActualValue == null) ||  
                (x.ActualValue is DBNull && y.ActualValue is DBNull))  
                return 0;  
  
            if (x.ActualValue == null || x.ActualValue is DBNull)  
  
                return -1;  
            return 1;  
        }  
  
        if (x.ActualValue is string)  
        {  
            return String.Compare(x.ActualValue.ToString(), y.ActualValue.ToString());  
        }  
        if (x.ActualValue is double)  
        {  
            if ((double)x.ActualValue < (double)y.ActualValue)  
                return -1;  
            return (double)x.ActualValue > (double)y.ActualValue ? 1 : 0;  
        }  
        if (x.ActualValue is decimal)  
        {  
            if ((decimal)x.ActualValue < (decimal)y.ActualValue)  
                return -1;  
            return (decimal)x.ActualValue > (decimal)y.ActualValue ? 1 : 0;  
        }  
        if (x.ActualValue is DateTime)  
        {  
            if ((DateTime)x.ActualValue < (DateTime)y.ActualValue)  
                return -1;  
            return (DateTime)x.ActualValue > (DateTime)y.ActualValue ? 1 : 0;  
        }  
        if (x.ActualValue is TimeSpan)  
        {  
            if ((TimeSpan)x.ActualValue < (TimeSpan)y.ActualValue)  
                return -1;  
            return (TimeSpan)x.ActualValue > (TimeSpan)y.ActualValue ? 1 : 0;  
        }  
        if (x.ActualValue.GetHashCode() < y.ActualValue.GetHashCode())  
            return -1;  
        return x.ActualValue.GetHashCode() > y.ActualValue.GetHashCode() ? 1 : 0;  
    }  
}  
  
  
Please find the sample from the below location.  
Regards,
Deivaselvan 


Loader.
Up arrow icon