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

Multibinding

7 Answers 244 Views
GridView
This is a migrated thread and some comments may be shown as answers.
david ocasio
Top achievements
Rank 1
david ocasio asked on 09 Oct 2009, 02:00 PM
Is it possible to combine values from two columns to show one column
i.e

if the data  has a "first name" field  and a "last name" fiel
i want to show in the grid a column called "fullname" which is the combination of those two

i can make an sql view with the combination of those columns to feed to the grid but id rather not

thanks
dco


7 Answers, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 13 Oct 2009, 12:24 PM
Hi david ocasio,

You can easily do this by using an IValueConverter. Create a binding to the business object itself and the in the converter return the concatenated value like this:

<UserControl x:Class="TicketID_249361_Multibinding.MainPage"
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    xmlns:local="clr-namespace:TicketID_249361_Multibinding"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid>
        <Grid.Resources>
            <local:ClubToInfoConverter x:Key="ClubToInfoConverter"/>
        </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"
                        DataMemberBinding="{Binding StadiumCapacity}" DataFormatString="{}{0:N0}">
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Info"
                        DataMemberBinding="{Binding Path=., Converter={StaticResource ClubToInfoConverter}}">
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

And the converter that does the magic:

using System;
using System.Globalization;
using System.Windows.Data;
 
namespace TicketID_249361_Multibinding
{
    public class ClubToInfoConverter : IValueConverter
    {
        #region IValueConverter Members
 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var club = (Club) value;
            return string.Format("{0} was founded in {1}."
                                 , club.Name
                                 , club.Established.Year);
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
 
        #endregion
    }
}

I have attached a sample project. I hope this helps.

Sincerely yours,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
david ocasio
Top achievements
Rank 1
answered on 13 Oct 2009, 12:42 PM
thanks
that works great

thats the concept i was missing
passing the datacontext to the converter 
with the "path=."

again it works great
thankyou very much
0
Pankaj
Top achievements
Rank 1
answered on 25 May 2011, 05:31 AM
Hi Ross,

I tried your solution but it gave me object reference error. 'value' arguement  was set to null. Please suggest.
0
Lauren
Top achievements
Rank 1
answered on 27 Sep 2011, 10:27 PM
Hi,

I'm trying to do something very similar:
my radgridview's itemsource = List<Object>
Let's say Object has 4 properties: p1, p2, p3, p4
p1 and p2 are directly bound to two columns.
for the 3rd column, depending on teh string value of p3, i want to display p4, else i want to keep the cell empty.

I'mdoing what described in a previous post
DataMemberBinding="{Binding Path=., Converter={StaticResource ClubToInfoConverter}}"
but the following exception keeps coming: "two way binding requires path"

How can i solve this ?
thanks.

0
Daní
Top achievements
Rank 1
answered on 28 Sep 2011, 12:19 PM
My two cents about the solution supplied by Ross:

The solution will work fine only if the properties involved are constant, so they won't never change. In fact, it is not realizing real multibinding. The binding won't never be aware for changes in "Name" or "Stablished.Year" properties. This approach is usefull in several scenarios like this, but unusefull in many others. Silverlight lacks for multibinding support built in, however, fortunately, you can find many MultiBinding implementations for silverlight just googling that can solve all scenarios. I have my own multibinding implementation based on http://www.scottlogic.co.uk/blog/colin/2010/05/silverlight-multibinding-solution-for-silverlight-4/ that I can share with every one interested.
0
Lauren
Top achievements
Rank 1
answered on 28 Sep 2011, 02:35 PM
Thanks Dani.
You're right, i hink the issue here is that i want to be able to insert rows into my radgridview.
Binding works the first time, but when i add a new row and insert a value for p4, it fails with the exception "two way binding requires path"

Any solution to achieve this with RadGridView ?
Thank you.
0
Daní
Top achievements
Rank 1
answered on 28 Sep 2011, 03:10 PM
Hi Lauren,

Multibing is complex and requires loads of stuff (xaml + IMultivalueConverter). I only use Multibinding as the last option. I don't know how your scenario exactly is. In many situations similars to the one you expose I often add an extra property, let's call p5. p5 is a readonly property that returns p4 o null depending on p3 value. Below I'm pasting an example:
public class MyViewModel: ViewModelBase
    {
        private string _p3;
                 
        public string p3
        {
            get
            {
                return this._p3;
            }
            set
            {
                if (this._p3 != value)
                {
                    this._p3 = value;
                    this.OnPropertyChanged("p3");
                    this.OnPropertyChanged("p5");
                }
            }
        }
 
        private string _p4;
                 
        public string p4
        {
            get
            {
                return this._p4;
            }
            set
            {
                if (this._p4 != value)
                {
                    this._p4 = value;
                    this.OnPropertyChanged("p4");
                }
            }
        }
 
        public string p5
        {
            get { return string.IsNullOrEmpty(p3) ? string.Empty : p4; }
        }
    }

Now you can bind the 3rd column to p5 property. Whenever p3 property changes it raises the PropertyChanged event for both p3 and p5 properties, so UI will be notified. 
Tags
GridView
Asked by
david ocasio
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
david ocasio
Top achievements
Rank 1
Pankaj
Top achievements
Rank 1
Lauren
Top achievements
Rank 1
Daní
Top achievements
Rank 1
Share this question
or