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

[Solved] No method 'Sum' on type 'System.Linq.Enumerable' is compatible with the supplied arguments.

4 Answers 172 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 22 Sep 2009, 12:11 AM
Hi,

I'm attempting to implement Totals for the GridView control.  I have the GridView binding to an object at run-time and I have implemented all the necessary interfaces to get it sort and filter properly.

public class OlapValue : IComparable<OlapValue>, IEquatable<OlapValue>, IComparable 

Now I would like to be able to total the column.  Of course, the grid has no idea what to do because it's bound to a complex type and so it throws an error.

I've tried to boil it down to a simple example available here.

Is there a Sum method I need to add to my Type (OlapValue)?

4 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 23 Sep 2009, 01:22 PM
Hi Rick,

We use the Sum methods that are provided by Linq and those methods are only available for simple types like int, double, etc. Unfrotunately it is not possible to provide your own Sum method.

You could try a different approach - instead of binding directly to the  VeryComplexType you can bind to VeryComplexType.ValueAsDouble.

uxRadGridView.Columns.Add(CreateColumn("ValueComplexType.ValueAsDouble""Complex Type"true)); 

That way you will not have to implement IComparable, IEquatable, etc and all operation like sorting, filtering, and grouping will work out of the box.

Kind regards,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Rick Glos
Top achievements
Rank 1
answered on 23 Sep 2009, 06:16 PM
I wish I could make use of a simple type Milan.

Unfortunately, the values need to be formatted differently.  Columns can be Percent, Currency, Number and within those formats, sometimes the user wants 0 decimal places, 2 decimal places, etc.  Also, based on whether the value is negative or positive, I am formatting the BorderBrush and Background of the GridViewCell.

Here's a screenshot with play data.  Note the different column formats and colors.

I have achieved this by defining a Style on the RadGridView.Resources

<telerikControlsGridView:RadGridView.Resources> 
    <Style TargetType="telerikGridView:GridViewCell" x:Name="HighlightNegativeValueAndFormatValue"
        <Setter Property="Template"
            <Setter.Value> 
                <ControlTemplate TargetType="telerikGridView:GridViewCell"
                    <Border DataContext="{TemplateBinding Value}"  
                            BorderThickness="{TemplateBinding BorderThickness}"  
                            BorderBrush="{Binding Converter={StaticResource ColorFormatterBorderBrush}}"  
                            Background="{Binding Converter={StaticResource ColorFormatterBackground}}"
                        <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right" VerticalAlignment="Center"
                            <TextBlock Text="{Binding Converter={StaticResource ValueFormatter}}" /> 
                        </StackPanel> 
                    </Border> 
                </ControlTemplate> 
            </Setter.Value> 
        </Setter> 
        <Setter Property="BorderThickness" Value="0,0,1,1"/> 
    </Style> 
</telerikControlsGridView:RadGridView.Resources> 




My original post concerning this is here.

Then in the ValueFormatter and ColorFormatter classes mentioned in the above code I cast the object value as the OlapValue so I can use the complex type ConverterParmater property.

    public class ValueFormatter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            SDG.Client.Library.ViewModels.MerchandiseGridViewModel.OlapValue olapValue = value as SDG.Client.Library.ViewModels.MerchandiseGridViewModel.OlapValue; 
            return string.Format(olapValue.ConverterParameter, olapValue.Value); 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
    } 


    public class ColorFormatter : IValueConverter 
    { 
        public Brush DefaultBrush { getset; } 
        public Brush HighlightBrush { getset; } 
 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            SDG.Client.Library.ViewModels.MerchandiseGridViewModel.OlapValue olapValue = value as SDG.Client.Library.ViewModels.MerchandiseGridViewModel.OlapValue; 
 
            if ((double)olapValue.Value < 0) 
            { 
                return HighlightBrush; 
            } 
            else 
            { 
                return DefaultBrush; 
            } 
 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            throw new NotImplementedException("This Convert supports only OneWay binding"); 
        } 
    } 

So you can see my hesitation to rip all that out just to get SumFunction working.  Perhaps you have a better solution in mind?



0
Milan
Telerik team
answered on 24 Sep 2009, 09:49 AM
Hello Rick Glos,

You could easily eliminate the ValueFormatter class by using the DataFormatString property of our GridViewDataColumn. For example, to display a value as percentage you could try the following :

<telerik:GridViewDataColumn Header="Number"   
                            DataMemberBinding="{Binding Number}" 
                            DataFormatString="{}{0:0%}"

As far I can see you can keep the ColorFormatter.

All the best,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Rick Glos
Top achievements
Rank 1
answered on 25 Sep 2009, 04:07 PM
Hi Milan,

Thanks for your input and looking into this.

I'm not comfortable ripping out all that code just at the moment as I believe I may need to do more complex formatting in the future (i.e. only highlight values for Column A if they are +/- 5%  and +/- 10 % for Column B, etc).  I have a suspicion that I may need to heavily use the complex type (OlapValue) for more than just the simple color and formatting I have implemented now.

However, I was able to solve the problem.  I just created a separate property of a simple type on the enumerable object I'm binding the grid to. So I just created a Double property and put the same value that is in the OlapValue.ValusAsDouble.  Then I just bind the AggregateFunction to that separate property instead of the one that is being used to display the values.

Allowing the AggregateFunction to define the SourceField and not just have it bind to the same column that it is placed within was some excellent foresight by Telerik.  Thanks for that.
Tags
GridView
Asked by
Rick Glos
Top achievements
Rank 1
Answers by
Milan
Telerik team
Rick Glos
Top achievements
Rank 1
Share this question
or