Can't get combobox to bind to a custom object in a GridView

4 posts, 0 answers
  1. Chris Kirkman
    Chris Kirkman avatar
    101 posts
    Member since:
    Apr 2010

    Posted 31 Jan 2018 Link to this post

    I've tried to put this question in the forum but the page keeps disappearing after I click "submit", so here I am...

    I have a 3 column GridView and the GridViewComboBoxColumn is not binding correctly.  I've set the following for the grid.

    1. DisplayMember = Color
    2. ValueMember = Id 
    3. DataType =  System.ComponentModel.BindingList`1[TelerikGridViewTests.Models.ColorViewModel]

    When I run the application I see "System.ComponentModel.BindingList`1[TelerikGridViewTests.Models.ColorViewModel]" instead of the object within my model.  For my other 2 columns in the grid (a decimal and a string) those are displayed just fine. 

     

    Here is my model code... 

    using System.ComponentModel;
    
    namespace TelerikGridViewTests.Models
    {
        public class CarsViewModel : ViewModelBase
        {
            public CarsViewModel()
            {
                _cars = new BindingList<CarViewModel>();
    
                _cars.Add(new CarViewModel()
                {
                    Id = 0m,
                    Description = "Ford",
                    Colors = new ColorsViewModel().Colors,
                });
    
                _cars.Add(new CarViewModel()
                {
                    Id = 1m,
                    Description = "Chevy",
                    Colors = new ColorsViewModel().Colors,
                });
    
                _cars.Add(new CarViewModel()
                {
                    Id = 2m,
                    Description = "Dodge",
                    Colors = new ColorsViewModel().Colors,
                });
            }
    
            BindingList<CarViewModel> _cars;
            public BindingList<CarViewModel> Cars { get { return _cars; } set { _cars = value; } }
        }
    
        public class CarViewModel : ViewModelBase
        {
            public decimal Id { get; set; }
            public string Description { get; set; }
    
            BindingList<ColorViewModel> _colors;
            public BindingList<ColorViewModel> Colors
            {
                get { return _colors; }
                set { _colors = value; }
            }
        }
    
        public class ColorsViewModel : ViewModelBase
        {
            public ColorsViewModel()
            {
                Colors = new BindingList<ColorViewModel>();
    
                Colors.Add(new ColorViewModel()
                {
                    Id = 1,
                    Color = "Blue",
                });
                Colors.Add(new ColorViewModel()
                {
                    Id = 2,
                    Color = "Red",
                });
                Colors.Add(new ColorViewModel()
                {
                    Id = 3,
                    Color = "Green",
                });
                Colors.Add(new ColorViewModel()
                {
                    Id = 4,
                    Color = "Yellow",
                });
            }
    
            public BindingList<ColorViewModel> Colors { get; set; }
        }
    
        public class ColorViewModel : ViewModelBase
        {
            public int Id { get; set; }
            public string Color { get; set; }
        }
    }

     

    and code which binds the model..

    using System.ComponentModel;
    using System.Windows.Forms;
    using TelerikGridViewTests.Models;
    
    namespace TelerikGridViewTests
    {
        public partial class Form1 : Form
        {        
            public Form1()
            {
                InitializeComponent();
    
                BindingList<CarViewModel> _cars = new CarsViewModel().Cars;
                radGridView1.DataSource = _cars;
            }
        }
    }

  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3758 posts

    Posted 01 Feb 2018 Link to this post

    Hello, Chris,

    Thank you for writing.  

    The provided sample project is greatly appreciated. The Id and Description properties are of simple types but the Colors property is a BindingList of custom objects. In this case RadGridView will show the type. Note that GridViewComboBoxColumn needs a DataSource collection and the cells should store the values specified as ValueMember of the column. The following help article demonstrates how to setup properly the GridViewComboBoxColumn: https://docs.telerik.com/devtools/winforms/gridview/columns/column-types/gridviewcomboboxcolumn

    Alternatively, you can use a GridViewTextBoxColumn and handle the EditorRequired event where you can specify what editor to be used. Here is a sample code snippet: 
    public Form1()
      {
          InitializeComponent();
     
          BindingList<CarViewModel> _cars = new CarsViewModel().Cars;
          radGridView1.DataSource = _cars;
     
          this.radGridView1.Columns[2].IsVisible = false;
          GridViewTextBoxColumn col = new GridViewTextBoxColumn("MyColumn");
          this.radGridView1.Columns.Add(col);
          this.radGridView1.EditorRequired += radGridView1_EditorRequired;
      }
     
      private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
      {
          if (this.radGridView1.CurrentColumn.Name == "MyColumn")
          {
              RadDropDownListEditor ddl = new RadDropDownListEditor();
              RadDropDownListEditorElement element = ddl.EditorElement as RadDropDownListEditorElement;
              element.DataSource = ((CarViewModel)this.radGridView1.CurrentRow.DataBoundItem).Colors;
              element.DisplayMember = "Color";
              e.Editor = ddl;
          }
      }


    I hope this information helps. Should you have further questions I would be glad to help. 
     
     Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Chris Kirkman
    Chris Kirkman avatar
    101 posts
    Member since:
    Apr 2010

    Posted 01 Feb 2018 Link to this post

    The 2nd method you provided...using the EditorRequired event allowed me to get it to work as I needed.  Thanks.  You might want to push this info to the forums, it wasn't entirely straightforward even with the documentation.
  4. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3758 posts

    Posted 02 Feb 2018 Link to this post

    Hello, Chris,  

    Thank you for writing back. 

    I am glad that the provided solution was useful for your case. I am converting this support ticket into a forum thread in order the community to benefit from it. 

    If you have any additional questions, please let me know. 

     Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top