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

How can I manually update Grouped Aggregates?

2 Answers 157 Views
GridView
This is a migrated thread and some comments may be shown as answers.
LHR
Top achievements
Rank 1
LHR asked on 08 Aug 2012, 07:59 PM
I currently have a RadGridView that is bound to a collection of objects. One of those objects contains a property that is calculated based on a user-supplied value, so whenever the user changes the value, the value in every row changes as well.

I am able to manually update the Footer Aggregate using .CalculateAggregates(), however if the Grid is grouped, this does not update the aggregates that are located in the group totals.

How can I make the RadGridView re-calculate the aggregates in the grouped records? The relevant bits of code are below

<TextBox Text="{Binding SalePercent}" />

<
telerik:RadGridView x:Name="MyGrid" DataContextChanged="MyGrid_DataContextChanged"
           ItemsSource="{Binding MyObservableCollection}" ShowColumnFooters="True">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Price, StringFormat=N2}" Header="Price">
            <telerik:GridViewDataColumn.AggregateFunctions>
                <telerik:SumFunction Caption="Price: " ResultFormatString="{}{0:C}" />
            </telerik:GridViewDataColumn.AggregateFunctions>
        </telerik:GridViewDataColumn>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding SalePrice, StringFormat=N2}" Header="Sale Price">
            <telerik:GridViewDataColumn.AggregateFunctions>
                <telerik:SumFunction Caption="Sale Price: " ResultFormatString="{}{0:C}" />
            </telerik:GridViewDataColumn.AggregateFunctions>
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

private
void MyGrid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    MyViewModel vm = MyGrid.DataContext as MyViewModel;
    if (vm != null)
        vm.PropertyChanged += MyViewModel_PropertyChanged;
 
    MyGrid.DataContextChanged -= MyGrid_DataContextChanged;
}
 
void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "SalePercent")
        MyGrid.CalculateAggregates();
}
void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "SalePercent":
            foreach (var item in MyObservableCollection)
                item.SalePrice = item.Price * SalePercent;
            break;
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Vlad
Telerik team
answered on 09 Aug 2012, 05:40 AM
Hello,

 Have you tried to call Rebind() instead CalculateAggregates() in your case?

Kind regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
LHR
Top achievements
Rank 1
answered on 09 Aug 2012, 11:51 AM
Thank you Vlad, that works great
Tags
GridView
Asked by
LHR
Top achievements
Rank 1
Answers by
Vlad
Telerik team
LHR
Top achievements
Rank 1
Share this question
or