Problem with displaymember in combobox column

10 posts, 0 answers
  1. John
    John avatar
    32 posts
    Member since:
    Sep 2011

    Posted 01 Sep 2011 Link to this post

    I have a telerik gridview

        <UserControl x:Class="TelerikGridViewComboBoxExample.MainPage"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            d:DesignHeight="300" d:DesignWidth="400"
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
       
            <Grid x:Name="LayoutRoot" Background="White">
                <Grid.RowDefinitions>
                    <RowDefinition Height="143*" />
                    <RowDefinition Height="157*" />
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0">
                    <TextBlock Text="Good Sample"/>
                    <telerik:RadGridView x:Name="radGridView"
                                AutoGenerateColumns="False" ItemsSource="{Binding Peoples}">
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewComboBoxColumn
                            DataMemberBinding="{Binding CountryID}"
                            UniqueName="Country"
                            SelectedValueMemberPath="Id"
                            DisplayMemberPath="Name"/>
                           
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName}" UniqueName="First Name"/>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding LastName}" UniqueName="Last Name"/>
                        </telerik:RadGridView.Columns>
                    </telerik:RadGridView>
                </StackPanel>
               
            </Grid>
        </UserControl>

    and here is a xaml.cs

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Net;
        using System.Windows;
        using System.Windows.Controls;
        using System.Windows.Documents;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Windows.Media.Animation;
        using System.Windows.Shapes;
        using System.Collections.ObjectModel;
        using Telerik.Windows.Controls;
       
        namespace TelerikGridViewComboBoxExample
        {
            public partial class MainPage : UserControl
            {
                public MainPage()
                {
                    InitializeComponent();
                    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
                }
       
                void MainPage_Loaded(object sender, RoutedEventArgs e)
                {
                    foreach (var x in
                       new People[] {
                        new People { CountryID = 0, FirstName = "Sebastain", LastName = "Vettel" },
                        new People { CountryID = 1, FirstName = "Fernando", LastName = "Alonso" },
                        new People { CountryID = 2, FirstName = "James", LastName = "Button" }
                        })
                    {
                        Peoples.Add(x);
                    }
       
       
                    foreach (var x in
                    new Country[] {
                        new Country { Id = 0, Name = "Germany",Nationality = "german"},
                        new Country { Id = 1, Name = "Spain" ,Nationality = "Spanish"},
                        new Country { Id = 2, Name = "UK" ,Nationality = "English"}
                        })
                    {
                        Countries.Add(x);
                    }
       
                    this.DataContext = this;
                    ((GridViewComboBoxColumn)this.radGridView.Columns["Country"]).ItemsSource = Countries;
                }
       
                private ObservableCollection<People> peoples = new ObservableCollection<People>();
                public ObservableCollection<People> Peoples
                {
                    get { return peoples; }
                    set { peoples = value; }
                }
       
                private ObservableCollection<Country> countries = new ObservableCollection<Country>();
                public ObservableCollection<Country> Countries
                {
                    get { return countries; }
                    set { countries = value; }
                }
            }
       
            public class People
            {
                public int CountryID { get; set; }
                public string FirstName { get; set; }
                public string LastName { get; set; }
            }
       
            public class Country
            {
                public int Id { get; set; }
                public string Name { get; set; }
                public string Nationality { get; set; }
            }
        }

    Everything works ok, but I want to display in countries column value Nationality, but as now I want in combo names of countries to choose.

    so :
    1. user clicks at row in Country column ,
    2. selects Germany
    3.value in row is Germnan.

    if it is not possilbe, I want to have in that row first, and last letter of name of country(for example "gy")

    I'm using 2010.1.603.1040 version of telerik silverlight pack.

    Is it possible?

    Best regards

  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 02 Sep 2011 Link to this post

    Hello John,

    Please take a look at this article. Do you want to achieve something like in the example at the end of it - the one accomplished with ItemTemplate ?
    Or do you want to display countries values in the DropDown, but visualize the Nationality in the row once you commit the change ? 
     

    Kind regards,
    Maya
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. John
    John avatar
    32 posts
    Member since:
    Sep 2011

    Posted 02 Sep 2011 Link to this post

    Thanks for your reply Maya,

    I want the same effect as at first image with combobox column, but UK in combo should be Great Britain, and in cell should be British.

    Best regards
  4. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 02 Sep 2011 Link to this post

    Hello John,

    You may define CellEditTemplate for the GridViewComboBoxColumn and set a RadComboBox inside. Its DisplayMemberPath should be set to Country, while the one of the column itself to Nationality. For example:

    <telerik:GridViewComboBoxColumn DataMemberBinding="{Binding ClubID}"                                                                               
                                                    SelectedValueMemberPath="ID"
                                                    ItemsSource="{Binding Clubs, Source={StaticResource MyViewModel}}"
                                                    DisplayMemberPath="Name" >
                        <telerik:GridViewComboBoxColumn.CellEditTemplate>
                            <DataTemplate>
                                <telerik:RadComboBox ItemsSource="{Binding Clubs, Source={StaticResource MyViewModel}}"
                                                     DisplayMemberPath="ID"
                                                     SelectedValue="{Binding ClubID}"
                                                     SelectedValuePath="ID" />
                            </DataTemplate>
                        </telerik:GridViewComboBoxColumn.CellEditTemplate>
                    </telerik:GridViewComboBoxColumn>


     

    Best wishes,
    Maya
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  5. John
    John avatar
    32 posts
    Member since:
    Sep 2011

    Posted 02 Sep 2011 Link to this post

    Could You attach me some example? I can figure out, how to merge it with my project.

    Best regards
  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 02 Sep 2011 Link to this post

    Hi John,

    I am attaching the sample project here. Please take a look at it and let me know if you need any further assistance. 

    Best wishes,
    Maya
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  7. John
    John avatar
    32 posts
    Member since:
    Sep 2011

    Posted 02 Sep 2011 Link to this post

    I've made my example in paint :)

    The Blues is a The Blues is a common name of Chelsea Londony.
    For Manchester United would be Red Devils.

    It is possilbe that few clubs have the same common name.

    Sorry, for my bad english. This is a source of our misunderstanding I belive.
  8. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Sep 2011 Link to this post

    Hi John,

    Well, in this case you may define the GridViewComboBoxColumn as follows:

    <telerik:GridViewComboBoxColumn DataMemberBinding="{Binding ClubID}"                                                                               
                                                    SelectedValueMemberPath="ID"
                                                    ItemsSource="{Binding Clubs, Source={StaticResource MyViewModel}}"
                                                    DisplayMemberPath="Name" >
                        <telerik:GridViewComboBoxColumn.CellEditTemplate>
                            <DataTemplate>
                                <telerik:RadComboBox ItemsSource="{Binding Clubs, Source={StaticResource MyViewModel}}"
                                                     DisplayMemberPath="Name"
                                                     SelectedValue="{Binding ClubID}"
                                                     SelectedValuePath="ID" >
                                    <telerik:RadComboBox.SelectionBoxTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding ID}" />
                                        </DataTemplate>
                                    </telerik:RadComboBox.SelectionBoxTemplate>
                                </telerik:RadComboBox>
                            </DataTemplate>
                        </telerik:GridViewComboBoxColumn.CellEditTemplate>
                    </telerik:GridViewComboBoxColumn>

     

    Regards,
    Maya
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  9. John
    John avatar
    32 posts
    Member since:
    Sep 2011

    Posted 03 Sep 2011 Link to this post

    Great, we are really close :)

    When, I select item from combo, value in cell is changing, but...
    when I click somewhere else(I leave cell) then value is changing to official name.
  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 05 Sep 2011 Link to this post

    Hi John,

    You may play along with the properties of GridViewComboBoxColumn, RadComboBox and TextBlock defined in the SelectionBoxTemplate. I would definitely recommend you to walk through our documentation for complete understanding of those elements - the column and RadComboBox. Once yo grasp the general idea, you will be able freely to modify them so that they correspond to your requirements. 
     

    All the best,
    Maya
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Back to Top