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

Aggregate Functions for selected Rows

4 Answers 286 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 19 Jul 2011, 08:12 AM
Hi,

is there a way to implement the Count and Average Functions only for selected Rows?

Regards,
Thomas

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 20 Jul 2011, 01:01 PM
Hi Thomas,

Generally the Aggregate Function is applied to all the items in the GridView.

Regarding your question, it would be better to implement your own logic for calculating the column footer based on the selection changed. You may subscribe for the SelectionChanged event handler and place your logic there.

Is this solution good for your case?

Greetings,
Didie
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Thomas
Top achievements
Rank 1
answered on 20 Jul 2011, 01:36 PM
Hi Didie,

the problem is, that i am using MVVM here. All my logic is in the ViewModel and  i would like to avoid the Selected Event in this case.

At first i tried to bind the IsSelected property from the Row to my ViewModel to get all selected items, but this did not work correctly (i think because of virtualization). Is this right or missed i something? This is my favoured approach, i would be very grateful if you can give me a solution for this approach.

<telerik:RadGridView.RowStyle>
   <Style TargetType="telerik:GridViewRow">
      <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay,   
             UpdateSourceTrigger=PropertyChanged}"/>
    </Style>
</telerik:RadGridView.RowStyle>

If i decide to use the Selected Event in the view how can i easily implement this aggregation? Currently my footer look like this:

<telerik:GridViewDataColumn.Footer>
     <StackPanel TextElement.FontStyle="Italic">
         <StackPanel Orientation="Horizontal">
                <Label Content="Avg:" />
                 <Label  Margin="5,0,0,0" Content="{Binding Path=DataContext.OriginalAverage, RelativeSource={RelativeSource
                           Mode=FindAncestor, AncestorType=telerik:RadGridView}}"></Label>
              </StackPanel>
           <StackPanel Orientation="Horizontal">
               <Label Content="Sum:" />
               <Label Margin="5,0,0,0" Content="{Binding Path=DataContext.OriginalSum, RelativeSource={RelativeSource
                     Mode=FindAncestor, AncestorType=telerik:RadGridView}}"></Label>
             </StackPanel>
    </StackPanel>
</telerik:GridViewDataColumn.Footer>
0
Dimitrina
Telerik team
answered on 21 Jul 2011, 05:04 PM
Hi Thomas,

In case you do want to avoid using events, then you may use a Behavior. You will need to add a SelectedItems property to your ViewModel, which you associate to the SelectedItems of the GridView through the Behavior.
The  code in the ViewModel would be like:

private ObservableCollection<object> selectedItems;
 
public ObservableCollection<object> SelectedItems
{
    get { return selectedItems; }
    set
    {
        selectedItems = value;
        selectedItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(selectedItems_CollectionChanged);
    }
}
 
void selectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    //Update aggregate results.
 
    if (SelectedItems.Count != 0)
    {
        Sum = SelectedItems.OfType<Player>().Select(c => c.Number).Sum();
        Average = SelectedItems.OfType<Player>().Select(c => c.Number).Average();
    }
    else
    {
        Sum = 0;
        Average = 0;
    }
}


I am attaching a sample project illustrating this approach.
Regards,
Didie
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Thomas
Top achievements
Rank 1
answered on 22 Jul 2011, 10:41 AM
Thank you, this helps a lot!!!
Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Thomas
Top achievements
Rank 1
Share this question
or