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

Calculated columns

11 Answers 604 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Daniel Nevers
Top achievements
Rank 1
Daniel Nevers asked on 20 Jul 2009, 08:04 AM
Hello all,

I am new to WPF and RadControls and I am trying to use the RadGridView right now (reauirements of a new customer).

I bound my RadGridView to a simple collection of object. Displaying properties of objects works like a charm.
Anyway, I wasn't able to find how to create a calculated column based on on or several properties of the objetcs in the bound collection!

I searched the forum, the documentation and found nothing (even for the classic MS DataGrid)!

Is there any known working solution to create calculated data columns?
(be aware that it will not be possible for me to add the calclated field directly in the objects of the collection)

Thank you,
Best regards,

Daniel

11 Answers, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 20 Jul 2009, 08:37 AM
Hello Daniel Nevers,

This can be easily achieved by harnessing the power of the IValueConverter. I have prepared a small sample project that demonstrates how to do this. My business entity is a football club and has a name, date of establishment, stadium capacity and ticket price. We will add a calculated column which will show the ticket revenue that the clubs will get when the park is completely sold out. Here is the XAML defining the "normal" columns and finally the calculated one:

<Window x:Class="TicketID_228472_CalculatedColumns.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
    xmlns:local="clr-namespace:TicketID_228472_CalculatedColumns" 
    Title="Window1"
    <Grid> 
        <Grid.Resources> 
            <local:ClubToSelloutRevenueConverter x:Key="clubToSelloutRevenueConverter"/> 
        </Grid.Resources> 
        <telerik:RadGridView  
            Name="clubsGrid"  
            Grid.Row="0" 
            AutoGenerateColumns="False" 
            ColumnsWidthMode="Auto"
            <telerik:RadGridView.Columns> 
                <telerik:GridViewDataColumn  
                    Header="Name"  
                    DataMemberBinding="{Binding Name}"
                </telerik:GridViewDataColumn> 
                <telerik:GridViewDataColumn  
                    Header="Est."  
                    DataMemberBinding="{Binding Established}" 
                    DataFormatString="{}{0:yyyy}"
                </telerik:GridViewDataColumn> 
                <telerik:GridViewDataColumn  
                    Header="Stadium Capacity"  
                    DataMemberBinding="{Binding StadiumCapacity}" 
                    DataFormatString="{}{0:N0}"
                </telerik:GridViewDataColumn> 
                <telerik:GridViewDataColumn  
                    Header="Ticket Price"  
                    DataMemberBinding="{Binding TicketPrice}" 
                    DataFormatString="{}{0:C2}"
                </telerik:GridViewDataColumn> 
                <telerik:GridViewDataColumn  
                    Header="Sellout Revenue"  
                    DataMemberBinding="{Binding Path=., Converter={StaticResource clubToSelloutRevenueConverter}}" 
                    DataFormatString="{}{0:C2}"
                </telerik:GridViewDataColumn> 
            </telerik:RadGridView.Columns> 
        </telerik:RadGridView> 
    </Grid> 
</Window> 
 

Each "normal" column is bound to a "normal" property, "{Binding Name}" for example. The calculated column "Sellout Revenue" on the other hand is bound to the whole Club business object: "{Binding Path=., Converter={StaticResource clubToSelloutRevenueConverter}" and lets the converter do the calculation which is the stadium capacity times the ticket price:

using System; 
using System.Globalization; 
using System.Windows.Data; 
 
namespace TicketID_228472_CalculatedColumns 
    internal class ClubToSelloutRevenueConverter : IValueConverter 
    { 
        #region IValueConverter Members 
 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            var club = value as Club; 
            if (club == null
            { 
                throw new ArgumentException("value must be a Club""value"); 
            } 
 
            return club.StadiumCapacity*club.TicketPrice; 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
        #endregion 
    } 

A binding combined with an appropriate converter can let you do virtually anything. Please, see the attached project for the complete source code.

I hope this helps.

Sincerely yours,
Ross
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
Daniel Nevers
Top achievements
Rank 1
answered on 20 Jul 2009, 08:49 AM
Hello Ross,

First of all, thank you for this really quick answer!
And best part is probably the fact that the proposed solution fits exactly my needs! :-)

Indeed, converters looks like a really powerfull solution. I'll test it right now.

Thank you very much,

Daniel
0
Rossen Hristov
Telerik team
answered on 20 Jul 2009, 08:56 AM
Hi Daniel Nevers,

Please, do not hesitate to contact us again if you have any other questions or difficulties.

Regards,
Ross
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
Greg
Top achievements
Rank 2
answered on 22 Feb 2010, 07:34 PM
This is a really clever approach but there is one drawback: how do you prevent the designer from refusing to render the xaml due a "value must be a Club" unhandled exception?

Regards,
Greg Lee
0
Rossen Hristov
Telerik team
answered on 24 Feb 2010, 09:30 AM
Hello Greg,

You can remove the exception if you would like.

All the best,
Ross
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Gilbert Figueroa
Top achievements
Rank 1
answered on 16 Feb 2011, 11:17 PM
How to enable Filtering and sorting for Calculated Column. Setting IsSortable and IsFilterable are not taking effect.
0
Milan
Telerik team
answered on 22 Feb 2011, 08:40 AM
Hi Gilbert Figueroa,

Currently filtering and sorting on such columns is not available. We will be adding this functionality in a future release. Unfortunately I cannot give you any concrete time frame. 


Best wishes,
Milan
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Gilbert Figueroa
Top achievements
Rank 1
answered on 22 Feb 2011, 02:16 PM
Some days ago, someone from Telerik give this link for other threat and it works for me, it doesn't have filter but sort functionality works
http://www.telerik.com/community/forums/silverlight/gridview/filter-and-aggregate-functions-with-gridviewexpressioncolumns.aspx

Thank you.
0
Yordanka
Telerik team
answered on 22 Feb 2011, 02:46 PM
Hello Gilbert Figueroa,

Sorry for the confusion. You can sort an expression column and by default IsSortable="True". You can check Calculated Column demo for a reference.
 
Best wishes,
Yordanka
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Andrey
Top achievements
Rank 1
answered on 16 Mar 2011, 08:26 PM
Same problem.
I am trying to make reusable duration column, something like this:

public class GridViewDurationColumn : GridViewDataColumn
{
    public DateTime Start
    {
        get { return (DateTime)GetValue(StartProperty); }
        set { SetValue(StartProperty, value); }
    }
 
    public static readonly DependencyProperty StartProperty =
        DependencyProperty.Register("Start", typeof(DateTime), typeof(GridViewDurationColumn), new PropertyMetadata(null));
 
    public DateTime End
    {
        get { return (DateTime)GetValue(EndProperty); }
        set { SetValue(EndProperty, value); }
    }
 
    public static readonly DependencyProperty EndProperty =
        DependencyProperty.Register("End", typeof(DateTime), typeof(GridViewDurationColumn), new PropertyMetadata(null));
 
    public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
    {
        var duration = End - Start;
        return new TextBlock() { Text = duration.ToString() };
    }
 
}
and in Xaml
<local:GridViewDurationColumn Start="{Binding StartDate}" End="{Binding EndDate}" IsReadOnly="True" />
but it doesn't work. Start and End properties are never set.
Converter is a good approach, but if i want to make it reusable i have to pass Start and End to ConverterParameter and then make reflection magic. i don't think that it is a good idea. Please help! Thanks
0
Yavor Georgiev
Telerik team
answered on 16 Mar 2011, 09:02 PM
Hello Andrey,

 Could you please open a separate thread about your issue, detailing the version of RadControls you use, the platform (WPF, Silverlight)? Thank you.

Best wishes,
Yavor Georgiev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
Daniel Nevers
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Daniel Nevers
Top achievements
Rank 1
Greg
Top achievements
Rank 2
Gilbert Figueroa
Top achievements
Rank 1
Milan
Telerik team
Yordanka
Telerik team
Andrey
Top achievements
Rank 1
Yavor Georgiev
Telerik team
Share this question
or