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

Filter not working when bound to an object

10 Answers 433 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rick Glos
Top achievements
Rank 1
Rick Glos asked on 26 May 2009, 10:59 PM
I've read this forum post and this blog post.  Similar to the original poster, I have an object (MyObject) I'd like to use rather than a base type (double, string, etc) for the column type.

I've got sorting working by implementing IComparable, but the filter icon is not appearing on the column headers.

Is there an interface I need to implement to enable the filtering mechanism?

I tried overriding ToString on the object but that did not seem to help.


10 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 28 May 2009, 09:05 AM
Hi Rick,

In order to filter on custom object you should also implement IEquatable<> interface. I have prepared a small sample project that shows this in practice. Please find it in the attached archive.

Hope this helps.

Sincerely yours,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Stefan Dobrev
Telerik team
answered on 28 May 2009, 12:00 PM
My bad Rick,

In a hurry I have created the sample application in WPF. Although the code is the same I have prepared a Silverlight example as well. Please find it in the new attachment.

Sorry for the inconvenience.

Sincerely yours,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Rick Glos
Top achievements
Rank 1
answered on 02 Jun 2009, 11:24 PM
Hi Stefan,

Sorry it took me awhile to get back to this.  I had to work on another project.

You solution does work when you are binding directly to a list of objects, but it does not work if I use the DataTable as the ItemsSource.  I'm not sure why as the datatable is just and object as well.

I have modified your solution slightly so that you can see what I mean.  I don't see a place to upload files here but I have uploaded it on SkyDrive for you here.

When you run it, you will see that you cannot sort your list of messages nor can you filter them.

You can sort if you add the IComparable interface (not the Generic one) to your class...  perhaps you can give me more detail as to what is happening and how to work around it.
0
Rick Glos
Top achievements
Rank 1
answered on 03 Jun 2009, 06:42 PM
I had another idea this morning.  Generate the type on the fly.  You'll see I've created a page3.xaml that generates a class called DynamicRow, and then I generate properties on that type so that I can bind the GridView to a IList of DynamicRows.

It works mostly - I get sorting (although I need to add IsCustomSortingEnabled="True" ) and I get filtering.  My problem with the filtering is that it assumes the value is text so it does not add filtering options like 'Is less then'.

Here's a link to a screenshot of what I'm attempting to achieve.

Here's a link to a screenshot of the problem I am having.

And here's a link to the source code - I used your same project again.

I downloaded the Telerik Silveright Source code and tried to figure out what I needed to implement but I can't figure it out.  I need help.

Long story short - I want to bind to an object because the user has control of the formatting (Currency, Number, 1 decimals, 2 decimals, etc).  I'm using your GridView control to sit on top of a OLAP Cube.  The user has the power to create a 'view' which basically allows them to return 1, 2, 3 ... n columns from the OLAP Cube.  I don't want to many type up the XAML for 100's of columns and I don't want to bind to a object that has hundreds of properties.  I'm storing these values in a table and I want to iterate over them and generate it on the fly.  Does this make sense?  Any help would be appreciated.
0
Stefan Dobrev
Telerik team
answered on 04 Jun 2009, 08:31 AM
Hi Rick,

Straight to your questions. In order to make the GridView filtering display all options in the combo box the column's DataType should be some numeric type (double in your case). Also your DataMemberBinding should have the correct property path that points to the double value of your OlapValue object.

I have modified your project in order to accomplish all this. Please find it in the attached archived file.

Hope this helps.

Sincerely yours,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Rick Glos
Top achievements
Rank 1
answered on 04 Jun 2009, 05:48 PM
Hi Stefan,

Thanks for having a look at the source code.

Unfortunately I cannot bind directly to the double value because not every column is of currency format.

Therefore I created an OlapValue object so I could pass in the value and converter into the formatter so that I can change the formatting based on the ConverterParameter.  

For example:

Quantity On Hand = {0:N}
Sales Dollars Variance Percent = {0:P}
Weeks of Supply = {0:N2}

Your suggestion of setting the DataType is excellent and appears to solve the problem on the surface.

Since I will be building up the GridViewDataColumn's programmatically, I figured I'll just set the DataType to typeof(double) for each one.  Then I can still pass in the OlapValue object into the different Converter objects and format the value based on that.  So I added code for that via a MockView.  Latest code is here.

It works, but if actually try to perform a filter (i.e. filter the SalesDollars) , I get:

    No coercion operator is defined between types 'FilteringByCustomTypeSilverlight.OlapValue' and 'System.Double'.
    
    at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
    at System.Linq.Expressions.Expression.Convert(Expression expression, Type type)
    at Telerik.Windows.Data.Expressions.FilterDescriptorExpressionBuilder.CreateMemberExpression()
    at Telerik.Windows.Data.Expressions.FilterDescriptorExpressionBuilder.CreateBodyExpression()
    at Telerik.Windows.Data.Expressions.FieldFilterDescriptionExpressionBuilder.CreateExpression(FilterDescriptor descriptor)
    ...
    at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
    at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
    at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

Seems like there is a interface or operator I need to implement to tell it how to convert the OlapValue to a double?






0
Accepted
Stefan Dobrev
Telerik team
answered on 05 Jun 2009, 09:19 AM
Hi Rick,

I have managed to do this. You will need to implement implicit conversion operator from OvapValue to double. Here is the code:
public class OlapValue : IComparable<OlapValue>, IEquatable<OlapValue> 
    ... 
 
    public static implicit operator double(OlapValue olapValue) 
    { 
        return olapValue.ValueAsDouble; 
    } 
     
    ... 

Hope this helps.

All the best,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Rick Glos
Top achievements
Rank 1
answered on 05 Jun 2009, 03:09 PM
It works!  Thank you Stefan.

Thanks for working through this with me.  I now have a working solution that I can use to implement in our production application.  I've yet to use an implicit operators in my career - great to come across them finally.

Currently I have almost 1000 lines of XAML code defining every type of column our users can see in our production application and I'm setting the visibility property and reording them at run-time.  With the users adding more columns each day, it's becoming unmanageable.  This solution will reduce those lines to about 10 lines of XAML code and the columns can now be generated on the fly.

For anyone else reading that might need something similar (a grid to sit on top of a SQL or OLAP query that has columns where the user can define what columns are in the query (or view as they might call it)) here's some background on what's in the source code:

This project generates (emits) an object based on a definition in a backing store (database) and then generates the columns in the GridView so that the grid can bind directly to a list of those objects.  This would be an alternative to a DataTable mechanism mentioned here.

I have cleaned up our working project to just the necessary files, objects, etc.  You can find the source here.
0
Mike
Top achievements
Rank 1
answered on 07 Sep 2011, 05:05 AM
I realize this is an old thread, but I am running into a similar problem with the RadGridView. When filtering, I get a similar error "No coercion operator is defined between types 'System.Int32' and 'System.String'" . Similar to Rick, I am using custom column generation based on the search criteria, and am returning a 'SearchResult' object.

I tried to implement the OLAP conversion per Stefan, but I am a little unsure how to call this conversion. To make things messy my project is in VB, not C#. I am also using the Q2 Silverlight controls if it makes a difference.

Thanks...
0
Rossen Hristov
Telerik team
answered on 07 Sep 2011, 10:28 AM
Hi Mike,

Can you please try to provide more details about why this coercion error comes up? What is the type of the column? What are the values that are inside the column? What type are you filtering on? Anything else that you consider important would be more than welcome.

It would be best if you can send us a small stripped down dummy version of your project that shows this exception, so we can actually debug it.

All the best,

Ross
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Rick Glos
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
Rick Glos
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Rossen Hristov
Telerik team
Share this question
or