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

Row filtering problem

5 Answers 100 Views
GridView
This is a migrated thread and some comments may be shown as answers.
stephen
Top achievements
Rank 1
stephen asked on 04 Oct 2011, 07:19 AM
Hi,

I have a RadGridView bound to an observablecollection of dataItems, each of which has a IsReadOnly properly. If the dataItem object is readonly, I would like to hide the row in the grid unless a checkbox is checked (the checkbox is elsewhere on the control). I cannot change my bound collection, as the order of the collection is important, and there is a 2 way binding from some columns in my grid which rely on the row number and tab number being the same.

I would like to do it in the code behind, such that if a row has the readonly bool set in the datacontext (viewmodel), the grid itself filters it out. Searching these forums for a simple example of how to do this seems to indicate that custom row filtering has been removed from the latest version of radgridview. Is this true? Is there a simple workaround you know of?

thanks,
Stephen 

5 Answers, 1 is accepted

Sort by
0
stephen
Top achievements
Rank 1
answered on 05 Oct 2011, 12:09 AM
Just to clarify, 

I don't want to filter by a column value of my grid - I want to filter by a member of the data object which the grid is bound to, which is not shown on the grid.

 so if my class looks like this

public class DataItem : ViewModelBase
 {
       private string _name;
        private string _maximum;
        private string _minimum;
        private string _newValue;
        private string _isReadOnly;
   etc
}
 
and the first 4 properties are columns in the grid, _isReadOnly is not, but if it is set (it implements INotifyPropertyChanged), I want my grid to be filtered.

the example here: http://blogs.telerik.com/rossenhristov/posts/09-11-20/custom_filtering_with_radgridview_for_silverlight.aspx
 
seems to rely on the column that you want to filter by being passed in to the Prepare method. I don't want to filter by a column. Is it possible to have a hidden column which isnt shown in the grid? 

Also, my filter control will just be a checkbox outside of the grid - is that possible? Or does the control have to be inside of the grid control?

Thanks
 
0
stephen
Top achievements
Rank 1
answered on 05 Oct 2011, 06:56 AM
I've just seen this sample on how to do it with a hidden column. 

http://www.telerik.com/community/forums/silverlight/gridview/filter-on-hidden-column.aspx

I think this might be my answer. I've added a column which is bound to my bool, and set visible to false. However, the sample CustomFilterDescriptor takes in all the columns in my grid, and i think it searches all columns for a search term.

Is there a simpler way to do it on one column, where if the value in this hidden column is false, dont show that row? I guess i could search for the string "False" or "True".

Also, the above sample assumes you can access the name of your grid by name. I can't because it is inside a ContentTemplate.
eg:
<telerikG:RadTabControl.ContentTemplate>
   <telerikG:RadGridView 
                                        blah blah
                                        Name="DataUIGridView"
    >

Is there a way to get at the grid from the code behind file without traversing the whole VisualTree using VisualTreeHelper which I think is very bad?
0
stephen
Top achievements
Rank 1
answered on 05 Oct 2011, 07:21 AM
I suppose I could instantiate the CustomFilterDescriptor in the XAML instead of doing it in the code behind like this:

<telerikG:RadTabControl.ContentTemplate>
<telerikG:RadGridView> 
 <telerikG:RadGridView.FilterDescriptors>
                                            <DataUI:CustomFilterDescriptor >
                                                
                                            </DataUI:CustomFilterDescriptor>
                                        </telerikG:RadGridView.FilterDescriptors>

but how do you pass in all the columns into the constructor?

Or maybe I can use the GridLoaded event or something similar in which i can store a reference to the grid in the code behind? like this:

 private void DataUIGridView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            _gridView = sender as RadGridView;


        }
0
stephen
Top achievements
Rank 1
answered on 06 Oct 2011, 12:37 AM
Using the GridLoaded event to store the grid seemed to solve my problem of passing the columns into the CustomFilterDescriptor. When I click my checkbox or button however, I am doing this:

 private void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
        {
            this.CustomFilterDescriptor.FilterValue = "False";
        }


this was causing a problem in the above example in the DefaultValue method called by UpdateCompositeFilterValues when it tries to convert the type of the FilterDescriptor (type is null):

 private static object DefaultValue(Type type)
        {
            if (type.IsValueType)
            {
                return Activator.CreateInstance(type);
            }
            return null;
        }


I decided to try not converting the value, since it is just a string search anyway for False, and replaced the UpdateCompositeFilterValues method with this:

 foreach (FilterDescriptor descriptor in this.compositeFilterDesriptor.FilterDescriptors)
            {
                descriptor.Value = this.FilterValue;
            }

This got rid of the errors, but my grid is not being filtered by false values when i press my button. Is it because the button is not in the grid?
0
stephen
Top achievements
Rank 1
answered on 07 Oct 2011, 06:00 AM
I finally got to the bottom of it - this is the code i used in my code behind to filter by the hidden column with the uniqueName "IsReadOnly":

private void GridView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            _gridView = sender as RadGridView;
            _gridView.FilterDescriptors.Clear();


            if (_showNonAdjustableChecked)
            {
                ColumnFilterDescriptor readOnlyFilter = new ColumnFilterDescriptor((IDataFieldDescriptor)_gridView.Columns["IsReadOnly"]);
                readOnlyFilter.DistinctFilter.DistinctValues.Add(true);
                readOnlyFilter.DistinctFilter.DistinctValues.Add(false);
                _gridView.FilterDescriptors.Add(readOnlyFilter);
            }
            else
            {
                ColumnFilterDescriptor readOnlyFilter = new ColumnFilterDescriptor((IDataFieldDescriptor)_gridView.Columns["IsReadOnly"]);
                readOnlyFilter.DistinctFilter.DistinctValues.Add(false);
                _gridView.FilterDescriptors.Add(readOnlyFilter);
            }


        }
Tags
GridView
Asked by
stephen
Top achievements
Rank 1
Answers by
stephen
Top achievements
Rank 1
Share this question
or