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

Custom aggregate function to count where property is true

6 Answers 304 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tormod
Top achievements
Rank 2
Tormod asked on 13 Mar 2012, 01:24 PM
Hi.

I try to create a custom aggregate function.

I get this exception.
"No generic method 'SimCandidateCount' on type 'GenerateFieldEmulator.SimCandidateCounter' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic."

Can you either spot what is wrong or tell me how to find out what the failing overload resolution information is?

xaml:

 

 

 

<telerik:GridViewColumn.AggregateFunctions>

 

 

 

 

<local:SimCount SourceField="SimPossible" SourceFieldType="{x:Type System:Boolean}" Caption="Candidate Modules : " />

 

 

 

 

</telerik:GridViewColumn.AggregateFunctions>


The SimCount class is here.

 

public

 

 

class SimCount : Telerik.Windows.Data.EnumerableSelectorAggregateFunction

 

 

 

 

 

{

 

 

    protected override string AggregateMethodName

 

    {

 

 

        get { return "SimCandidateCount"; }

 

    }

 

 

    protected override Type ExtensionMethodsType

 

    {

 

 

        get

 

 

 

 

 

        {

 

 

            return typeof(SimCandidateCounter);

 

        }

    }

 

}

 

 

public static class SimCandidateCounter

 

 

 

 

 

{

 

 

    public static int SimCandidateCount<TSource>(IEnumerable<TSource> source,Func<TSource,bool> selector )

 

    {

 

 

        return source.Select(selector).Count(b => b);

 

    }

}

6 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 14 Mar 2012, 04:01 PM
Hello Tormod ,

Can you please send me the implementation of the objects you use in the ItemsSource for RadGridView.
I will gather a small sample for you with your data objects.

Kind regards,
Pavel Pavlov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Tormod
Top achievements
Rank 2
answered on 16 Mar 2012, 09:17 AM
Hi.
Sorry for taking a while in responding.
The data objects have some dependencies due to the type of private fields..
If you could just show me on a simple data object.

enum gender{male,female};

class person
{
    string name {get;set;}
    gender sex {get;set;}
}

and then just show a custom aggregate function that display number of males, that would be good.
Or are you saying that you expect my code to work, and that we should be debugging it?
0
Pavel Pavlov
Telerik team
answered on 20 Mar 2012, 04:01 PM
Hello Tormod,

Please find the project attached.

Regards,
Pavel Pavlov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Tormod
Top achievements
Rank 2
answered on 21 Mar 2012, 11:57 AM
Thank you. That looks right.

Problem is that I have the grid within a datatemplate. AFAIK, I cannot do "RadGridView1.Columns[0]... in the constructor"


This is becoming a general WPF question rather than specific to the RadGridView control, but still.

Can you please show how you would go about creating this from markup? Is there any "WPF" way of dealing with accessing in xaml something created from the code behind?

Should I create the AggregateFunction in the code behind and then add it to the Resources collection of the window? But this would be difficult since both the window creation and resource consumption happen inside "initializecomponents"? I cannot "wedge" anything in between the two.

Maybe I can create an empty AggregateFunction in the Resources section of XAML and then retrieve it by name from the code behind and set its AggregationExpression property after InitializeComponents?
Or would I need to set it as a dynamic resource and then assign an entirely new AggregationFunction?
The best I think of for the moment is x:Static. But this will only help me when I don't need instance members, even though that is the case for the moment.
0
Accepted
Pavel Pavlov
Telerik team
answered on 21 Mar 2012, 01:38 PM
Hello Tormod,

There are many possible approaches to solve the case. Lets start with an easier one :
In your data template you can subscribe to the DataLoaded event of RadGridView. Then in the event handler you may perform any additional initialization e.g.  adding the aggregate function.

Regards,
Pavel Pavlov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Tormod
Top achievements
Rank 2
answered on 21 Mar 2012, 03:39 PM
I used the Loaded event instead of the DataLoaded event. The DataLoaded event is fired whenever you ungroup the datagridview. Altering the observablecollection of aggregatefunctions in a column when ungrouping causes an exception.

In your last example: 

If you move the aggregatefunction setup to an eventhandler fired from the DataLoaded event and then.

* Start the application.
* Drag a column to the grouping area.
* Then click the "x" on it to remove the grouping.
..... causes exception.

But using "Loaded" appears to work just fine. Thank you.

Tormod
Tags
GridView
Asked by
Tormod
Top achievements
Rank 2
Answers by
Pavel Pavlov
Telerik team
Tormod
Top achievements
Rank 2
Share this question
or