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

Combobox in Gridview

2 Answers 112 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 21 Sep 2009, 06:53 AM
Hi,

I have som problems with comoboboxes in gridview. Look at my example here: www.rays.se/telerik/slgrid.zip

I have a class Color with properties ColorId (INT) and ColorName (string).

I have a class House with properties HouseId(INT), HouseColor(Color) and HouseName (string).

These classes are in collections List<>.

I want to list all the houses in the grid and the column Color I want to pick a color from via a combobox.

I want to Edit.

I can polulate the combobox with

EditorSettings

= ComboBoxEditorSettings

But I cannot get the Color to display the name of the color.
If 2 houses have the same color and I change one of them the other is also changed (strange).
And the grid.itemsource doesn´t have the right value of the color after edit. Its all strange. I´m doing something wrong...

/R

 

2 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 23 Sep 2009, 11:28 AM
Hi Raymond,

For this to work you will have to override the ToString and Equals methods of the Color class.

The ToString method should be overridden so that the name of the color is correctly displayed in the grid while not in edit mode. But you can do it like you did by binding to HouseColor.ColorId.

The Equals method should be overridden so that the concrete color instance of each concrete house could be matched against an instance from the ComboBox's ItemsSource thus enabling the the combo to show the correct color of each house.

With those two methods overridden you will no longer need the DisplayMemberPath and SelectedValuePath properties to be set, since it is the Color instance itself that will be the selected value of the Combo.

Also, note that I have implemented the INotifyPropertyChanged on your House class and when you are selecting different values from the combo you will be able to see the effects immediately in the debug grid I have placed below.

<UserControl xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"  x:Class="SLGrid.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" xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" Width="640" Height="480"
  <Grid x:Name="LayoutRoot"
      <Grid.RowDefinitions> 
        <RowDefinition /> 
        <RowDefinition /> 
      </Grid.RowDefinitions> 
         
      <telerikGridView:RadGridView x:Name="grd"  
                                   Grid.Row="0" 
                                   AutoGenerateColumns="False"  
                                   ShowGroupPanel="False"
          <telerikGridView:RadGridView.Columns> 
              <telerikGridView:GridViewDataColumn DataMemberPath="HouseId" Header="House id"/> 
              <telerikGridView:GridViewDataColumn DataMemberPath="HouseColor" Header="Color"  /> 
              <telerikGridView:GridViewDataColumn DataMemberPath="HouseName" Header="House name"/> 
            </telerikGridView:RadGridView.Columns> 
      </telerikGridView:RadGridView> 
 
      <Controls:DataGrid x:Name="debugGrid" Grid.Row="1"/> 
 
    </Grid> 
</UserControl> 
 

And here is the code:

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Windows.Controls; 
using Telerik.Windows.Controls; 
 
namespace SLGrid 
    public partial class MainPage : UserControl 
    { 
        public MainPage() 
        { 
            this.InitializeComponent(); 
 
            var comboBoxEditorSettings = new ComboBoxEditorSettings(); 
            comboBoxEditorSettings.ItemsSource = GetColors();; 
            ((GridViewDataColumn) this.grd.Columns[1]).EditorSettings = comboBoxEditorSettings; 
 
            List<House> houseList = GetHouses(); 
            this.grd.ItemsSource = houseList; 
            this.debugGrid.ItemsSource = houseList; 
        } 
 
        private static List<House> GetHouses() 
        { 
            List<Color> colorList = GetColors(); 
 
            var list = new List<House>(); 
 
            list.Add(new House() 
                         { 
                             HouseColor = colorList.First(c => c.ColorName == "Red"), 
                             HouseId = 100, 
                             HouseName = "My house" 
                         }); 
            list.Add(new House() 
                         { 
                             HouseColor = colorList.First(c => c.ColorName == "Yellow"), 
                             HouseId = 101, 
                             HouseName = "Your house" 
                         }); 
            list.Add(new House() 
                         { 
                             HouseColor = colorList.First(c => c.ColorName == "Black"), 
                             HouseId = 102, 
                             HouseName = "Dads house" 
                         }); 
            list.Add(new House() 
                         { 
                             HouseColor = colorList.First(c => c.ColorName == "Red"), 
                             HouseId = 103, 
                             HouseName = "Moms house" 
                         }); 
 
            return list; 
        } 
 
        private static List<Color> GetColors() 
        { 
            var list = new List<Color>(); 
 
            list.Add(new Color() {ColorId = 1, ColorName = "Red"}); 
            list.Add(new Color() {ColorId = 2, ColorName = "Yellow"}); 
            list.Add(new Color() {ColorId = 3, ColorName = "Black"}); 
            list.Add(new Color() {ColorId = 4, ColorName = "White"}); 
 
            return list; 
        } 
    } 
 
    /// <summary> 
    ///  
    /// </summary> 
    public class Color 
    { 
        public int ColorId { getset; } 
        public string ColorName { getset; } 
 
        public override string ToString() 
        { 
            return this.ColorName; 
        } 
 
        public bool Equals(Color other) 
        { 
            if (ReferenceEquals(null, other)) return false
            if (ReferenceEquals(this, other)) return true
            return other.ColorId == this.ColorId; 
        } 
 
        public override bool Equals(object obj) 
        { 
            if (ReferenceEquals(null, obj)) return false
            if (ReferenceEquals(this, obj)) return true
            if (obj.GetType() != typeof (Color)) return false
            return this.Equals((Color) obj); 
        } 
 
        public override int GetHashCode() 
        { 
            return this.ColorId; 
        } 
    } 
 
    /// <summary> 
    ///  
    /// </summary> 
    public class House : INotifyPropertyChanged 
    { 
        private Color houseColor; 
        private int houseId; 
        private string houseName; 
 
        public int HouseId 
        { 
            get { return this.houseId; } 
            set 
            { 
                if (value != this.houseId) 
                { 
                    this.houseId = value; 
                    this.OnPropertyChanged("HouseId"); 
                } 
            } 
        } 
 
        public Color HouseColor 
        { 
            get { return this.houseColor; } 
            set 
            { 
                if (value != this.houseColor) 
                { 
                    this.houseColor = value; 
                    this.OnPropertyChanged("HouseColor"); 
                } 
            } 
        } 
 
        public string HouseName 
        { 
            get { return this.houseName; } 
            set 
            { 
                if (value != this.houseName) 
                { 
                    this.houseName = value; 
                    this.OnPropertyChanged("HouseName"); 
                } 
            } 
        } 
        #region INotifyPropertyChanged Members 
 
        public event PropertyChangedEventHandler PropertyChanged; 
        #endregion 
 
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) 
        { 
            PropertyChangedEventHandler handler = this.PropertyChanged; 
            if (handler != null
            { 
                handler(this, args); 
            } 
        } 
 
        private void OnPropertyChanged(string propertyName) 
        { 
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
        } 
    } 

For this implementation, please see the attached project.

I hope this helps.

Regards,
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
Raymond
Top achievements
Rank 1
answered on 23 Sep 2009, 01:14 PM
Hi,

Thank you for your answer, I really appreciate it!

The example works and how I only need to attach it to my real case - after I have understand it :)

/R
Tags
GridView
Asked by
Raymond
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Raymond
Top achievements
Rank 1
Share this question
or