Search code examples
c#wpfinfragisticsxamgrid

Removing operand from XamGrid Filter


I am new to WPF and Infragistics. I have a small WPF project with a XamGrid and a filter. I want to remove the equals operand from the filter list for one of the columns in the grid. I found this code online from the Infragistics forums:

FilterColumnSettings fcs = 
this.MyDataGrid.Columns.DataColumns["ProductID"].FilterColumnSettings;
fcs.RowFilterOperands.Remove(ComparisonOperator.Equals);

Which is supposed to do just that, but I cannot get it to work. When my application loads up, the equals filter option is still there. Am I calling the code from the wrong place? This is what I have written:

public MainWindow()
    {
        InitializeComponent();
        FilterColumnSettings fcs = this.xamGrid.Columns.DataColumns["ProductID"].FilterColumnSettings;
        fcs.RowFilterOperands.Remove(ComparisonOperator.Equals);
    }

And this is my XamGrid in my XAML code:

<ig:XamGrid 
        x:Name="xamGrid" 
        ItemsSource="{Binding}" 
        ColumnWidth="*" 
        AutoGenerateColumns="False" >

        <ig:XamGrid.FilteringSettings>
            <ig:FilteringSettings AllowFiltering="FilterRowTop" />
        </ig:XamGrid.FilteringSettings>

        <ig:XamGrid.Columns>
            <ig:TextColumn Key="ProductID" HeaderText="Product ID" />
        </ig:XamGrid.Columns>
</ig:XamGrid> 

Any help would be appreciated!

EDIT: When I added in the filter, I kept getting the following error:

System.NullReferenceException: 'Object reference not set to an instance of an object.' Infragistics.Controls.Grids.ReadOnlyKeyedColumnBaseCollection.this[string].get returned null.

To overcome this error, I followed the solution from this link: https://www.infragistics.com/community/forums/f/retired-products-and-controls/29815/allowfilterrow-top-infragistics-silverlight-requireemptyconstructorexception and the solution that worked for me is as follows:

this.xamGrid.DataObjectRequested += new EventHandler<DataObjectCreationEventArgs>(xamGrid_DataObjectRequested);

public void xamGrid_DataObjectRequested(object sender, DataObjectCreationEventArgs e)
    {
        if (e.ObjectType == typeof(DataRowView))
        {
            DataTable dt = new DataTable();
            DataRow r = dt.NewRow();
            dt.Rows.Add(r);
            DataRowView drv = dt.DefaultView[dt.Rows.IndexOf(r)];
            e.NewObject = drv;
        }
    }

This allowed me to use the filtering option in the XamGrid but could this be causing my issue with changing the filter operands?

Thanks!


Solution

  • I was able to solve my issue by adding an event listener like so:

    // Raise event for filter operands
    this.xamGrid.PopulatingColumnFilters += new EventHandler<PopulatingFiltersEventArgs>(XamGrid_PopulatingColumnFilters);
    

    This event handler links to a method in which I have the following code:

    FilterColumnSettings fcs = this.xamGrid.Columns.DataColumns["ProductID"].FilterColumnSettings;
    foreach (var fil in fcs.RowFilterOperands.ToList())
    {
        // Remove the filters that do no equal "contains"
        if (fil.ComparisonOperatorValue != ComparisonOperator.Contains)
        {
           fcs.RowFilterOperands.Remove(fil);
        }
    }
    

    This code iterates through all of the filter operands for the ProductID column and removes all the filters except the filter operand "contains". This means that when the user tries to filter the column, they can only filter using "contains" and search for ProductIDs that contain a certain string.