Confusing binding on GridViewComboBoxColumn

16 posts, 0 answers
  1. Drammy
    Drammy avatar
    38 posts
    Member since:
    Feb 2008

    Posted 03 Jan 2011 Link to this post

    Hi guys,

    I have a situation where a GridViewComboBoxColumn is not displaying the correct value...

    I have a Sex column (Male, Female) which I am using a ComboBox to set in a grid.  I have a Sex class with an "Id" property and a "Name" property and a SexOptions Collection<Sex> with 2 Sex objects: 'M', Male and 'F', Female.

    I have a Person RIA object coming from my db with a Sex column (nchar(1)) with a value of 'M' or 'F'.

    I am using MVVM and have a view model presenting an ObservableCollection<Person> to the View.

    My RadGridView has the ObservableCollection<Person> set as the ItemsSource, with a GridViewComboBoxColumn in the Columns node.

    The GridViewComboBoxColumn is bound to the Sex property on the Person entity.  I have also included a DataColumn which is also bound to the sex property to confirm binding is working succesfully - it is.

    <telerik:GridViewDataColumn Width="*"
                                Header="Sex2"
                                DataMemberBinding="{Binding Sex}"
                                IsSortable="True" />
    <telerik:GridViewComboBoxColumn Width="*"
                                    Header="Sex"
                                    DataMemberBinding="{Binding Sex}"
                                    IsSortable="True"
                                    DisplayMemberPath="Name"
                                    ItemsSource="{StaticResource SexOptions}"
                                    SelectedValueMemberPath="Id" />

    The GridViewComboBoxColumn has the correct items in it but won't select a value correctly.  For example...

    The DataColumn displays 'F' but the GridViewComboBoxColumn is blank.  I go to select a value from the GridViewComboBoxColumn and can see the options Male and Female.  I select Male and hit tab off the combobox, the DataColumn updates to 'M' indicating the binding is working, but the GridViewComboBoxColumn (now out of edit mode once again) displays nothing.


    I have been scratching my head with this for a couple of days now as I was loathe to have to change my datasource to make it work.  Unfortunately I have decided this is all I can do.

    I have therefore changed the Person table in SQL to be a nvarchar(16) and updated the values from 'F' to 'Female' and 'M' to 'Male'.  I have removed the Id property from the Sex class and have changed the binding's SelectedValueMemberPath to 'Name'.


    All works fine now.  I would argue however that this is perhaps a bug as - what's the point in having a SelectedValueMemberPath if I can only use the same value as is set in DisplayMemberPath?


    I would prefer not to have to store more in the db to get this working and would prefer to store just the nchar(1) - any ideas on a workaround?
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Jan 2011 Link to this post

    Hi Drammy,

    The property defined in the DataMemberBinding of the GridViewComboBoxColumn needs to be a part of the ItemsSource of the grid. Furthermore, it has to correspond to the property defined as the SelectedValueMemberPath. 
    I am sending you a sample project illustrating the implementation of the GridViewComboBoxColumn in RadGridView. Please take a look at it and let me know in case of any misunderstandings according to your requirements.

    Best wishes,
    Maya
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. DevCraft banner
  4. Drammy
    Drammy avatar
    38 posts
    Member since:
    Feb 2008

    Posted 03 Jan 2011 Link to this post

    Hi Maya,

    Thanks for the response - you're explanation is enough for me, it has verified my investigations.

    Why can't the SelectedValueMemberPath be different to the DisplayMemberPath?


    Edit:  just re-read your post...

    The DataMemberBinding value did correspond to the SelectedValueMemberPath in both situations I outlined in my initial post.  However, I found that if the SelectedValueMemberPath != DisplayMemberPath then the ComboBox wouldn't select the correct value correctly...
  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Jan 2011 Link to this post

    Hi Drammy,

    Generally, they are different. The SelectedValueMemberPath corresponds to the property from the ItemsSource of the GridViewComboBoxColumn corresponding to the property in the ItemsSource of the grid set as the DataMemberBinding. The DisplayMemberPath is set to a property from the ItemsSource of the column and defines the value to be shown in the ComboBox.

    Best wishes,
    Maya
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  6. Drammy
    Drammy avatar
    38 posts
    Member since:
    Feb 2008

    Posted 03 Jan 2011 Link to this post


  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 04 Jan 2011 Link to this post

    Hi Drammy,

    May you try to re-type your last post as it appears to be blank ?
     

    Regards,
    Maya
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  8. alex
    alex avatar
    1 posts
    Member since:
    Jun 2011

    Posted 12 Aug 2011 Link to this post

    Hello,

    you said "it has to correspond to the property defined as the SelectedValueMemberPath.". What do you mean by "correspond".

    Many thanks!
  9. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 17 Aug 2011 Link to this post

    Hi Alex,

    The meanings of the combo box settings are illustrated in this article.

    Best wishes,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  10. Dan
    Dan avatar
    1 posts
    Member since:
    Mar 2011

    Posted 24 Aug 2011 Link to this post

    I need help with binding in the XAML for the GridViewComboBoxColumn ItemsSource property.
    I'm fairly certain it simply doesn't work. Am I correct or can you provide another example that does work...

    I tested this extensively. I see you _can_ bind in the XAML to the RadGridView ItemsSource:
    <UserControl x:Class="RadComboBoxInRadGridView.MainPage"
        x:Name="ThisUC"
    ...>
            <telerikGrid:RadGridView ItemsSource="{Binding People, ElementName=ThisUC}"
    ...


    where I added the following property to the backing-class:
    public partial class MainPage : UserControl
    {
        ...
        private ObservableCollection<Person> _people;
        public ObservableCollection<Person> People
        {
            get
            {
                if (_people == null)
                {
                    _people = Person.getNames();
                }
                return _people;
            }
        }
    }

    I've also added this property:
    private List<Country> _countries;
    public List<Country> Countries
    {
        get
        {
            if (_countries == null)
            {
                _countries = Country.GetCountries();
            }
            return _countries;
        }
    }


    And I can get it to bind in the code:
     
    ((GridViewComboBoxColumn)this.RadGridView1.Columns[1]).ItemsSource = Countries; // Country.GetCountries();

    (note I commented out the old code to use a property I could bind to in the XAML)

    I tried:
    <telerikGrid:GridViewComboBoxColumn Header="Nationality"
                DataMemberBinding="{Binding CountryID, Mode=TwoWay}"
                DisplayMemberPath="Name"
                SelectedValueMemberPath="ID"
                ItemsSource="{Binding Countries, ElementName=ThisUC}" >                       
    </telerikGrid:GridViewComboBoxColumn>

    But nope. This doesn't bind.

    Tell me how this would work properly?

    Thanks,
      Dan Wygant
  11. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 25 Aug 2011 Link to this post

    Hi Dan,

    You may look at this article covering the same scenario as yours.
     

    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 >>

  12. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 20 Dec 2011 Link to this post

    hi,

    I just read the article, everything is according to the code in there and yet, I see the elements in the combo but the grid have no value
    and when I try to select one value of a combobox in a row, it disapear.  what am I doing wrong ?
  13. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 20 Dec 2011 Link to this post

    <telerik:GridViewComboBoxColumn DataMemberBinding="{Binding Property3, Mode=TwoWay}"
                                    SelectedValueMemberPath="Property1"
                                    DisplayMemberPath="Property2">

    Property1 is the id property of the object in the collection and Property2 is of course what I want to show in the combobox which the collection is set to the ItemsSource of the GridViewComboboxColumn in the Loaded event which implement INotifyPropertyChanged

    there's something I don't see
  14. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 20 Dec 2011 Link to this post

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        Persons = new ObservableCollection<Person>();
      
        Persons.Add(new Person() { Property1 = 1, Property2 = "Marc Roussel" });
        Persons.Add(new Person() { Property1 = 2, Property2 = "Daniel Nadon" });
        Persons.Add(new Person() { Property1 = 3, Property2 = "Stéphane Genest" });
        Persons.Add(new Person() { Property1 = 4, Property2 = "Marc Trudel" });
        Persons.Add(new Person() { Property1 = 5, Property2 = "Adriana Suarez" });
      
        ((GridViewComboBoxColumn)rgvTest.Columns["Property3"]).ItemsSource = Persons;
    }
  15. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 20 Dec 2011 Link to this post

    there's a missing piece in this article.  what is this :

    <UserControl.Resources>
        <local:MainPageViewModel x:Key="mainPageViewModel" />
    </UserControl.Resources>

    Also do I really have to bind the ItemsSource like in this article since the ItemsSource is bound in the Loaded event in code behind ?
    if this is necessary I don't know what is MainPageViewModel
  16. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 20 Dec 2011 Link to this post

    I'm using the solution #2 and yet my cell in the gird is empty
  17. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 21 Dec 2011 Link to this post

    Hi Marc Roussel,

    Please open up a support ticket and attach a sample project illustrating your settings. I will be glad to take a look at it and suggest how to resolve the issue you are struggling with.
     

    Best wishes,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top
DevCraft banner