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

Confusing binding on GridViewComboBoxColumn

15 Answers 271 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Drammy
Top achievements
Rank 1
Drammy asked on 03 Jan 2011, 01:59 PM
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?

15 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 03 Jan 2011, 02:36 PM
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
0
Drammy
Top achievements
Rank 1
answered on 03 Jan 2011, 02:48 PM
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...
0
Maya
Telerik team
answered on 03 Jan 2011, 03:24 PM
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
0
Drammy
Top achievements
Rank 1
answered on 03 Jan 2011, 07:58 PM

0
Maya
Telerik team
answered on 04 Jan 2011, 08:14 AM
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
0
alex
Top achievements
Rank 1
answered on 12 Aug 2011, 02:57 PM
Hello,

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

Many thanks!
0
Pavel Pavlov
Telerik team
answered on 17 Aug 2011, 01:09 PM
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 >>

0
Dan
Top achievements
Rank 1
answered on 24 Aug 2011, 05:57 PM
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
0
Maya
Telerik team
answered on 25 Aug 2011, 08:05 AM
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 >>

0
Marc Roussel
Top achievements
Rank 2
answered on 20 Dec 2011, 07:10 PM
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 ?
0
Marc Roussel
Top achievements
Rank 2
answered on 20 Dec 2011, 07:12 PM
<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
0
Marc Roussel
Top achievements
Rank 2
answered on 20 Dec 2011, 07:15 PM
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;
}
0
Marc Roussel
Top achievements
Rank 2
answered on 20 Dec 2011, 07:25 PM
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
0
Marc Roussel
Top achievements
Rank 2
answered on 20 Dec 2011, 07:28 PM
I'm using the solution #2 and yet my cell in the gird is empty
0
Maya
Telerik team
answered on 21 Dec 2011, 07:57 AM
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 >>

Tags
GridView
Asked by
Drammy
Top achievements
Rank 1
Answers by
Maya
Telerik team
Drammy
Top achievements
Rank 1
alex
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Dan
Top achievements
Rank 1
Marc Roussel
Top achievements
Rank 2
Share this question
or