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

[Solved] GridView combo box with enumeration values

8 Answers 266 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.
John
Top achievements
Rank 1
John asked on 09 Aug 2009, 01:20 PM

This is a corollary to one of my previous posts entitled ‘Nullable’ GridViewComboBoxColumn. Basically, I have a business object named Person. I am trying to bind a list of these Person business objects in a RadGridView. One of the properties in Person is an enumeration named PetType. I would like this column in the grid to be a combo box that will let the user pick from a list of possible PetType values.


At first, I was having trouble showing the desired display member (PetType.ToString()) after the user had selected a value. The column seemed to want to show the (integer) value of the enumeration (e.g. 1, 2, 3, etc.) instead of the string representation (‘Dog’, ‘Cat’, ‘Cow’ etc.). I made an attempt to remedy this by modifying my value converter. So, specifically...


My value converter looked like this, but the integer value was being displayed after a value was selected (not desired):


public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    if (value != null) 
    { 
      return (int)value; 
    } 
 
    return null; 
 }


I then modified my value converter to look like this. Now, the string value is being displayed after a value was selected (this is desired):


public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
   if (value != null) 
   { 
       return value.ToString(); 
   } 
 
  return null; 

So, the modification to my value converter is simple and seems to makes sense...

For the most, part this works great. However, there now seems to be a slight glitch when the user first enters edit mode. It seems that the binding is temporarily disrupted and the combo box’s value is cleared.  Once the user selects a value, everything looks and behaves correctly.


I have included a couple of screen captures that show exactly what I am experiencing (http://www.4shared.com/file/123920806/aec5f96/IntegerValue.html and http://www.4shared.com/file/123920806/aec5f96/StringValue.html). Also, the relevant portions of my code are below. I am curious if this is a bug? Am I simply leaving something out? Or is there a different approach overall that I should use?


I should mention that I am using Silverlight 2 and the Q2/2009 controls.

Here is my Person business object and PetType enum:

    public class Person 
    { 
        public Person(int age, string firstName, string lastName, PetType petType) 
        { 
            this.Age = age
            this.FirstName = firstName
            this.LastName = lastName
            this.PetType = petType
        } 
 
        public int Age { get; set; } 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
        public PetType PetType { get; set; } 
 
        public static List<Person> GetSampleListOfPersons() 
        { 
            var persons = new List<Person> 
                            { 
                                new Person(30, "John", "Smith", PetType.Cat), 
                                new Person(29, "Jane", "Smith", PetType.Cow), 
                                new Person(30, "Peter", "Smith", PetType.Dog) 
                            }; 
            return persons; 
        } 
    } 
 
    public enum PetType 
    { 
        Dog = 1
        Cat = 2
        Cow = 3 
    } 

This is how I created my grid:

            this.RadGridView1.AutoGenerateColumns = false
            this.RadGridView1.Columns.Add(new GridViewDataColumn() { UniqueName = "Age" }); 
            this.RadGridView1.Columns.Add(new GridViewDataColumn() { UniqueName = "FirstName" }); 
            this.RadGridView1.Columns.Add(new GridViewDataColumn() { UniqueName = "LastName" }); 
 
            GridViewDataColumn comboBoxColumn = new GridViewDataColumn() { UniqueName = "PetType" }; 
            
            comboBoxColumn.DataMemberBinding = new Binding()  
                {  
                    Path = new PropertyPath("PetType"),  
                    Mode = BindingMode.TwoWay, 
                    Converter = new ValueConverter() 
                }; 
 
            ComboBoxEditorSettings editorSettings = new ComboBoxEditorSettings(); 
             
            editorSettings.SelectedValuePath = "Key"
            editorSettings.DisplayMemberPath = "Value"
            editorSettings.ItemsSource = this.CreatePetTypeList(); 
            comboBoxColumn.EditorSettings = editorSettings
 
            this.RadGridView1.Columns.Add(comboBoxColumn); 
 
            this.RadGridView1.ItemsSource = Person.GetSampleListOfPersons(); 

The method CreatePetTypeList is defined as follows. ValueListItem is a simple class with a key(int)/value(string) pair.

public IEnumerable<ValueListItem> CreatePetTypeList() 
    List<ValueListItem> list = new List<ValueListItem>(); 
    list.Add(new ValueListItem() { Key = 1Value = PetType.Dog.ToString() }); 
    list.Add(new ValueListItem() { Key = 2Value = PetType.Cat.ToString() }); 
    list.Add(new ValueListItem() { Key = 3Value = PetType.Cow.ToString() }); 
 
    return list.AsEnumerable(); 



8 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 12 Aug 2009, 09:40 AM
Hello John,

I have modified (simplified) your example so that it works as you expect.
You can find the changes in the attached project.

Let me know if this helps you.

Kind regards,
Hristo
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
John
Top achievements
Rank 1
answered on 15 Aug 2009, 01:50 PM
Hristo,

I appreciate your prompt response.

I reviewed the attached sample, and it seems that the display member is still not showing up in the grid.

To clarify what I am trying to achieve:
Before or after a user interacts with the drop down, I would like the cell to display PetType.ToString() (or some other text to describe the selected item). Instead of the grid showing the values of 1, 2, or 3, I would like it to show e.g. "Dog", "Cat", "Cow".

Your sample correctly displays the display member when the combo box is expanded. However, I would like to see this before and after a value is selected. I hope that makes sense.

Thanks!
0
Pavel Pavlov
Telerik team
answered on 17 Aug 2009, 09:05 AM

Hello John,
I am a little bit confused. I have tested the example that Hristo has sent to you. To me it seems to work as expected. As you can see in the screen capture here the RadGridView accurately displays the text descriptions of the pets. And it does not display the numeric IDs even before and after the combo has expanded.

I believe I am missing something . As soon as I find what exactly is wrong with the example I will gladly provide you a solution.

Regards,

Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
John
Top achievements
Rank 1
answered on 18 Aug 2009, 02:01 AM
Hi Pavel,

I appreciate your quick response. You guys rock! By the way, thanks for the introduction to Jing!

The video capture you provided of the sample behaves exactly as I want. I am puzzled why it does not behave the same for me.

Here is a video capture of the sample running on my machine.

Do you have any thoughts?




0
Accepted
Pavel Pavlov
Telerik team
answered on 18 Aug 2009, 08:04 AM
Hello John,

Can you plese check which version of Silverlight are you using  I believe the code provided has some issues with SL2  and works fine with SL3. This may be the reason for the different results on my PC and yours.

Kind regards,
Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
John
Top achievements
Rank 1
answered on 18 Aug 2009, 12:20 PM
I am definitely using Silverlight 2. I am not exactly sure when we are making the move to Silverlight 3. Do you know of any work around that I could use in the meantime if there are issues?
0
Pavel Pavlov
Telerik team
answered on 19 Aug 2009, 09:18 AM
Hello John,
There is a kind of work around here. You may try to set the CellTemplate property of the column to a DataTemplate which contains a TextBlock. Then bind the Text property using your own IValueConverter which should convert the numeric values of the enumeration to their  string representations.

Let me know in case you have troubles implementing this. I will gladly provide an example.

Regards,
Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
John
Top achievements
Rank 1
answered on 29 Aug 2009, 04:38 PM
Upgraded to SL3 and the latest Telerik controls. Works great! Thanks!
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Hristo
Telerik team
John
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Share this question
or