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

Converting values for filtering and exporting

1 Answer 40 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Eli
Top achievements
Rank 1
Eli asked on 15 Apr 2013, 05:58 PM
Hello,

I have a RadGridView where I am autogenerating columns from a datatable. Some columns need a converter on the values, and the conversion logic is different from column to column.

I am using the following code in the autogenerating column event to set the template to an object with a textblock and the right converter.

                var cellStyle = new Style();
                Setter setter = new Setter();
                setter.Property = TemplateProperty;
                var template = new ControlTemplate();
                var index = "[" + ((RadGridView)sender).Columns.Count.ToString() + "]";
                FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(System.Windows.Controls.ListBox));

                var value = new MultiBinding();
                value.Bindings.Add(new Binding(index));
                var columnBinding = new Binding();
                columnBinding.Source = e.Column.Header.ToString();
                value.Bindings.Add(columnBinding);
                value.Converter = (IMultiValueConverter)this.FindResource("TupleCreator");

                spFactory.SetValue(DataContextProperty, value);
                spFactory.SetValue(TemplateProperty, (ControlTemplate)this.FindResource("DataTemplate"));

                template.VisualTree = spFactory;
                setter.Value = template;
                cellStyle.Setters.Add(setter);

                e.Column.CellStyle = cellStyle;

This works fine for actually viewing the cells in the grid, and using visualization so that not all of the conversions are done at once. What I need to be able to do is filter and sort by these values, as well as have them in the cells of export to excel.

I have seen something similar done in another control using an unbound column data event.

Thanks,

Eli

1 Answer, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 16 Apr 2013, 09:11 AM
Hello,

IValueConverters are used for UI purposes only and they cannot play any part in the data engine operations. This is explained in this help article.

If you need to filter or sort on a property, then this property has to exist on the business object because filtering and sorting are done via LINQ against the actual data in the source collection and IValueConverters cannot be incorporated in this operation. The LINQ query can ultimately go directly to SQL Server (thanks to a Query Provider) and that is why there cannot be any IValueConverters involved in the Where or OrderBy clauses. SQL Server cannot do a Where on something that does not exist in the table.

To achieve what you want you would have to shape your data accordingly on the DB layer before feeding it to RadGridView.

Greetings,
Rossen Hristov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

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