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

[Solved] IValueConverter With Multiple Columns

23 Answers 434 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.
Lee
Top achievements
Rank 1
Lee asked on 23 Jun 2010, 08:53 PM
Is it possible to use binding with IValueConverter to color a column based on values from two different cells?

For example, if you have a grid with two columns, "Valid", and "DueDate", and I want to color the cell for DueDate if the value is less than the current date AND if the Valid cell contains True.

Is there a way to do this in RowLoaded using IValueConverter?

23 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 24 Jun 2010, 06:38 AM
Hello,

 Why not use CellStyleSelector instead? Please check this demo for more info.

All the best,
Vlad
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
Lee
Top achievements
Rank 1
answered on 24 Jun 2010, 03:34 PM
I've looked at the demo and I'm not sure this is addressing the functionality that I need.  It still looks like it's only using the value from one cell to determine the coloring.  I'm trying to use the value from two different columns to determine the coloring for one cell.

In my example I have a grid with two columns, "Valid" and "DueDate", and I want to color the cell for "DueDate" if the date is less than the current date AND if "Valid" cell contains True.  If the "Valid" cell contains False, I don't want apply custom coloring to "DueDate".

Am I missing something in the demo that address this?  It looks like all the demo is doing is comparing the value against a constant and then returning True or False.  It's still only working with data from one cell.


0
Vlad
Telerik team
answered on 24 Jun 2010, 03:44 PM
Hello,

 In the SelectStyle method of the StyleSelector you have reference to the entire object (data item) and you can access any property to perform any calculation. You can check also MSDN and my blog post for more info about style selectors. 

Regards,
Vlad
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
Lee
Top achievements
Rank 1
answered on 28 Jun 2010, 04:28 PM
Thanks for the update.

I've tried to implement the style selector the way it is in the demo and it works fine as long as I don't enable row virtualization.  As soon as I enable row virtualization, the coloring gets screwed up as I scroll around the grid.

For example, if a cell in a row is not colored initially and then I scroll down in the list, then back up to the uncolored row/cell, the cell is now colored.

Here is my XAML RadGrid definition:
    <Style x:Key="LateDueOnDockStyle" TargetType="telerikPresentation:GridViewCell"
        <Setter Property="Background" Value="Red" /> 
    </Style> 
    <Style x:Key="NotLateDueOnDockStyle" TargetType="telerikPresentation:GridViewCell" > 
        <!--<Setter Property="Background" Value="Green" />--> 
    </Style> 
    <local:DueOnDockConveter x:Key="converter" /> 
    <local:ConditionalStyleSelector x:Key="DueOnDockSelector" ConditionConverter="{StaticResource converter}"
        <local:ConditionalStyleSelector.Rules> 
            <local:ConditionalStyleRule Style="{StaticResource LateDueOnDockStyle}"
                <local:ConditionalStyleRule.Value> 
                    <sys:Boolean>True</sys:Boolean> 
                </local:ConditionalStyleRule.Value> 
            </local:ConditionalStyleRule> 
            <local:ConditionalStyleRule Style="{StaticResource NotLateDueOnDockStyle}"
                <local:ConditionalStyleRule.Value> 
                    <sys:Boolean>False</sys:Boolean> 
                </local:ConditionalStyleRule.Value> 
            </local:ConditionalStyleRule> 
        </local:ConditionalStyleSelector.Rules> 
    </local:ConditionalStyleSelector> 
 
 
    <telerikGridView:RadGridView x:Name="radgridRouteView" ShowColumnFooters="False" RowIndicatorVisibility="Collapsed" Grid.Row="1" 
                                 AutoGenerateColumns="False" ShowGroupPanel="False" IsFilteringAllowed="False" 
                                 IsReadOnly="True" EnableRowVirtualization="True" 
                                 SelectionChanged="routeView_SelectionChanged" Style="{StaticResource DefaultRowStyle}"
        <telerikGridView:RadGridView.SortDescriptors> 
            <telerikData:SortDescriptor Member="Route" SortDirection="Ascending" /> 
        </telerikGridView:RadGridView.SortDescriptors> 
        <telerikGridView:RadGridView.Columns> 
            <telerikGridView:GridViewDataColumn Header="Route" HeaderTextAlignment="Center" UniqueName="Route" TextAlignment="Center" IsReadOnly="True" Width="100" /> 
            <telerikGridView:GridViewDataColumn Header="Trailer" HeaderTextAlignment="Center" UniqueName="TrailerNumber" TextAlignment="Center" IsReadOnly="True" Width="70" /> 
            <telerikGridView:GridViewDataColumn Header="Due On Dock" HeaderTextAlignment="Center" UniqueName="DueOnDock" TextAlignment="Center" IsReadOnly="True" DataFormatString="{}{0:HH:mm}" CellStyleSelector="{StaticResource DueOnDockSelector}" /> 
        </telerikGridView:RadGridView.Columns> 
 
        <telerikNavigation:RadContextMenu.ContextMenu> 
            <telerikNavigation:RadContextMenu Opened="RouteStopRadContextMenu_Opened" ItemClick="RouteStopRadContextMenu_ItemClick" > 
                <telerikNavigation:RadMenuItem x:Name="receiveRoute" Header="Receive Route" Icon="Images/delivery.png"  /> 
                <telerikNavigation:RadMenuItem x:Name="undoReceiveRoute" Header="Set Route Pending" Icon="Images/Edit_UndoHS.png"  /> 
                <telerikNavigation:RadMenuItem x:Name="noRoute" Header="Route Not Valid For Today"  /> 
                <telerikNavigation:RadMenuItem IsSeparator="True" /> 
                <telerikNavigation:RadMenuItem x:Name="refreshRouteView" Header="Refresh Route View" Icon="Images/RefreshDocViewHS.png" /> 
            </telerikNavigation:RadContextMenu> 
        </telerikNavigation:RadContextMenu.ContextMenu> 
 
    </telerikGridView:RadGridView> 
 

Here is my Converter:
 
using System.Windows.Data; 
using CAXDock_RiaServices.Web; 
using System.Collections.Generic; 
using System; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Media; 
 
#if !SILVERLIGHT 
using System.Windows.Controls; 
#else 
using Telerik.Windows.Controls; 
#endif 
 
namespace CAXDock_RiaServices 
    enum RouteStates 
    { 
        NotReceived, 
        Received, 
        NoStopForToday 
    } 
 
    public class DueOnDockConveter : IValueConverter 
    { 
        #region IValueConverter Members 
 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            DailyRoutePM routeInfo = value as DailyRoutePM; 
 
            if (routeInfo != null) 
            { 
                if (routeInfo.RouteState.Value == (int)RouteStates.NotReceived) 
                { 
                    return routeInfo.DueOnDock < DateTime.Now ? true : false; 
                } 
            } 
 
            return null; 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
 
        #endregion 
    } 
 
    public class ConditionalStyleSelector : StyleSelector 
    { 
        public override System.Windows.Style SelectStyle(object item, System.Windows.DependencyObject container) 
        { 
            object conditionValue = this.ConditionConverter.Convert(item, null, null, null); 
            foreach (ConditionalStyleRule rule in this.Rules) 
            { 
                if (Equals(rule.Value, conditionValue)) 
                { 
                    return rule.Style; 
                } 
            } 
 
 
            return base.SelectStyle(item, container); 
        } 
 
        List<ConditionalStyleRule> _Rules; 
        public List<ConditionalStyleRule> Rules 
        { 
            get 
            { 
                if (this._Rules == null) 
                { 
                    this._Rules = new List<ConditionalStyleRule>(); 
                } 
 
                return this._Rules; 
            } 
        } 
 
        IValueConverter _ConditionConverter; 
        public IValueConverter ConditionConverter 
        { 
            get 
            { 
                return this._ConditionConverter; 
            } 
            set 
            { 
                this._ConditionConverter = value
            } 
        } 
    } 
 
    public class ConditionalStyleRule 
    { 
        object _Value; 
        public object Value 
        { 
            get 
            { 
                return this._Value; 
            } 
            set 
            { 
                this._Value = value
            } 
        } 
 
        Style _Style; 
        public Style Style 
        { 
            get 
            { 
                return this._Style; 
            } 
            set 
            { 
                this._Style = value
            } 
        } 
    } 


Can you tell me what I'm doing wrong?
0
Milan
Telerik team
answered on 02 Jul 2010, 09:56 AM
Hello Lee Weisenberger,

Hum, that seems very strange. Is it convenient for you to send us the whole application so that we can debug it on our site?


Regards,
Milan
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
Lee
Top achievements
Rank 1
answered on 06 Jul 2010, 09:26 PM
I have a sample project that demonstrates this issue.  Should I attach it to this forum post or submit a support ticket?
0
Accepted
Vlad
Telerik team
answered on 07 Jul 2010, 06:56 AM
Hello,

 Attaching projects in the forums is not allowed! Please open support ticket.

All the best,
Vlad
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
Lee
Top achievements
Rank 1
answered on 07 Jul 2010, 01:04 PM
Thanks.  I submitted a support ticket (325707) for this issue.
0
Carlos Sesma
Top achievements
Rank 1
answered on 13 Jul 2010, 10:49 PM
Hello, in this code im accessing the value of other cells and passing the comparison result using CellFgBinding.ConverterParameter
you can pass actually any value by using this attribute.
im also using PriceToColorConverter class provided in the Telerik conditional formating example to apply the formating.
hope this help.

private void RadGrid_PrdSummary_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
        {           
            if (e.Row is GridViewRow)
            {
                System.Windows.Data.Binding CellFgBinding = new System.Windows.Data.Binding("UnHr");
                CellFgBinding.Converter = new PrdMonitorSL3.Web.PriceToColorConverter();
                bool isGreaterThanRate = (int.Parse(e.Row.Cells[3].Content.ToString()) < int.Parse(e.Row.Cells[5].Content.ToString()));
                CellFgBinding.ConverterParameter = isGreaterThanRate;
                e.Row.Cells.Last().SetBinding(GridViewCellBase.ForegroundProperty, CellFgBinding);
            }
        }
0
Ben
Top achievements
Rank 1
answered on 01 Oct 2010, 05:44 PM
Was there any resolution to the EnableRowVirtualization="True" issue?
I'm having the same issue using a CellStyleSelector.

Thanks - Ben
0
Vlad
Telerik team
answered on 04 Oct 2010, 07:33 AM
Hi Ben,

Can you post more info about the problem? You can check our demo here (rows virtualization in RadGridView is enabled by default).

Kind regards,
Vlad
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
Ben
Top achievements
Rank 1
answered on 07 Oct 2010, 03:57 PM
Hi Vlad, thanks for the reply.

I went back to using the RowLoaded event and everything works fine. 

My problem was I was saying:

if ( negative_value )
    color = red;

I added:

else
    color = black;

and everything works fine.

Thanks again - Ben
0
Juliana
Top achievements
Rank 1
answered on 08 Oct 2010, 01:51 PM
Hi,

I use CellStyleSelector as it is in the sample. But when I update the value on which the actual style depends, cell gets the style only after scrolling out of the screen and back. How can I force the gridview to apply a new style immediately?

Thanks,
Juliana
0
Yavor Georgiev
Telerik team
answered on 08 Oct 2010, 01:55 PM
Hi Juliana,

 We have modified the behavior of CellStyleSelector to update the style of the data object raised a PropertyChanged event for the property the column is bound to. You should be able to find the new functionality in this week's Internal Build, which will be available for download later today.

Greetings,
Yavor Georgiev
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
Juliana
Top achievements
Rank 1
answered on 11 Oct 2010, 08:22 AM
Hi Yavor,

I replaced all dlls with v. 2010.2.1008.1040. But behavior remained the same. Should I change anything in code?

Thanks,
Juliana
0
Vlad
Telerik team
answered on 11 Oct 2010, 08:32 AM
Hello,

 Can you verify if your object implements INotifyPropertyChanged properly?

Greetings,
Vlad
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
Juliana
Top achievements
Rank 1
answered on 11 Oct 2010, 09:07 AM
Hi Vlad,

 Actually neither ConditionalStyleSelector nor my Converter implements INotifyPropertyChanged. Could you provide an example, so that I could if everything is right?

Thanks,
Juliana
0
Vlad
Telerik team
answered on 11 Oct 2010, 09:14 AM
Hi,

 You do not need INotifyPropertyChanged for this - just for the data items in your collection. 

Kind regards,
Vlad
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
Juliana
Top achievements
Rank 1
answered on 11 Oct 2010, 09:23 AM
Hi Vlad,

I have binding in grid ItemsSource="{Binding CateringList}" and this property in my viewmodel

        private List<EMS.DtoLibrary.Catering> _CateringList;
        public List<EMS.DtoLibrary.Catering> CateringList
        {
            get
            {
                return _CateringList;
            }
            set
            {
                _CateringList = value;
                NotifyPropertyChanged("CateringList");
            }
        }

Juliana
0
Vlad
Telerik team
answered on 11 Oct 2010, 09:28 AM
Hello,

 Can you verify if EMS.DtoLibrary.Catering implements INotifyPropertyChanged?

All the best,
Vlad
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
Juliana
Top achievements
Rank 1
answered on 11 Oct 2010, 10:06 AM
Hi Vlad,

Now I see what you mean :) Yes, this class implements INotifyPropertyChanged. And when I change value in a cell, this particular cell repaints. But it's not enough in my case. The thing is that cell style depends on another cell value. So I changed value in one cell, and another cell should change its color. How can I force that cell to repaint?

Thanks,
Juliana
0
Yavor Georgiev
Telerik team
answered on 11 Oct 2010, 10:22 AM
Hi Juliana,

 Please find attached a sample solution. It uses the our latest internal build binaries, which have this functionality. The CellStyleSelector will be invoked every time the property of the column is mentioned in a PropertyChanged event. Thus if the style of the cell depends on another property, make sure you raise two PropertyChanged events, just like I have done in the Established property of the Club class.

Sincerely yours,
Yavor Georgiev
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
Juliana
Top achievements
Rank 1
answered on 11 Oct 2010, 11:32 AM
Hi Yavor,

Thanks for the sample. That is definitely what I need to put everything on its place.
Thank you both for the help.

Best  Regards,
Juliana
Tags
GridView
Asked by
Lee
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Lee
Top achievements
Rank 1
Milan
Telerik team
Carlos Sesma
Top achievements
Rank 1
Ben
Top achievements
Rank 1
Juliana
Top achievements
Rank 1
Yavor Georgiev
Telerik team
Share this question
or