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

Issue binding to a grid whose results are fully filtered out

3 Answers 83 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 29 Mar 2013, 01:21 PM
I have a dynamically bound RadGridView, constructed using this blog post, that displays a list of results based on user input. I have a column called "Job Code" that contains either a 1 or a 2. If I filter that column to only show rows where "Job Code" is 1, and then rebind the grid to only contain rows whose "Job Code" is 2, no rows display in the grid as expected, but the Column headers fail to render making it impossible for me to remove the filter.

Here is a sample project I have constructed to illustrate the problem: https://dl.dropbox.com/u/109257/RadGridViewDynamicBinding.zip

Just filter the "Job Code" field so only rows with a value of 1 display. Then click the button at the bottom to only bind rows where the "Job Code" field is set to "2". You should see what I'm talking about.

Is this a bug or expected functionality? I want to preserve a user's filter setting between grid updates, but this presents problems.

Thanks.

3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 29 Mar 2013, 02:28 PM
Hello,

Here is what happens.

Your columns are auto-generated. So they can only be generated if there is at least one item present because you are using dynamic object. When you filter by 1 and then bind the grid to rows that only contain 2 no rows are present and that's you the columns are not auto-generated. Again, if you used a normal class this would not be the case because we would read the properties from the generic class of the collection. With dynamics we need an actual instance in order to see what properties there are.

You have three options:

1. You can clear a column filter before rebinding as described here if you want to.

2. Define the columns in XAML like this:

<telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Job Code}"/>
</telerik:RadGridView.Columns>

3. Before rebinding save the filters. Rebind. Restore the filters. Save/load of filters is described here.

That's all I can think of as a solution for your case.

I hope this helps.

Greetings,
Rossen Hristov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Andrew
Top achievements
Rank 1
answered on 29 Mar 2013, 03:12 PM
1 and 2 aren't really options for us. As for 3, when should I restore the filters? On the RadGridView.DataLoaded event? I don't think that will work because the you will run into InvalidOperationExceptions when trying to modify the DistinctFilter collection: the whole "Cannot change ObservableCollection during a CollectionChanged or PropertyChanged event" message.
0
Rossen Hristov
Telerik team
answered on 02 Apr 2013, 09:38 AM
Hi,

Do you get such an exception if you restore the filters in the DataLoaded event handler or after the grid is re-bound in the the button click event handler?

In general, when you are restoring filters, you are suspending all notifications until all filters are fully restored and only after that the notifications are turned on and the grid applies the filters in one huge batch.
 
public static void LoadColumnFilters(Telerik.Windows.Controls.GridView.GridViewDataControl grid
            , IEnumerable<FilterSetting> savedSettings)
        {
            grid.FilterDescriptors.SuspendNotifications();
 
            foreach (FilterSetting setting in savedSettings)
            {
                Telerik.Windows.Controls.GridViewColumn column = grid.Columns[setting.ColumnUniqueName];
 
                Telerik.Windows.Controls.GridView.IColumnFilterDescriptor columnFilter = column.ColumnFilterDescriptor;
 
                foreach (object distinctValue in setting.SelectedDistinctValues)
                {
                    columnFilter.DistinctFilter.AddDistinctValue(distinctValue);
                }
 
                if (setting.Filter1 != null)
                {
                    columnFilter.FieldFilter.Filter1.Operator = setting.Filter1.Operator;
                    columnFilter.FieldFilter.Filter1.Value = setting.Filter1.Value;
                    columnFilter.FieldFilter.Filter1.IsCaseSensitive = setting.Filter1.IsCaseSensitive;
                }
 
                columnFilter.FieldFilter.LogicalOperator = setting.FieldFilterLogicalOperator;
 
                if (setting.Filter2 != null)
                {
                    columnFilter.FieldFilter.Filter2.Operator = setting.Filter2.Operator;
                    columnFilter.FieldFilter.Filter2.Value = setting.Filter2.Value;
                    columnFilter.FieldFilter.Filter2.IsCaseSensitive = setting.Filter2.IsCaseSensitive;
                }
            }
 
            grid.FilterDescriptors.ResumeNotifications();
        }

In your case, which collection complains about the "Cannot change ObservableCollection during a CollectionChanged or PropertyChanged event" when you do this?

Greetings,
Rossen Hristov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Andrew
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or