custom cell editing

5 posts, 1 answers
  1. Eric Moreau
    Eric Moreau avatar
    22 posts
    Member since:
    Sep 2007

    Posted 07 Dec 2010 Link to this post

    Hi

    I have a special requirement where all the cells of a particular column can have a different datatype (thus a different editor).

    I was able to put dates, strings and numbers but I have a problem with combo. My combo has a ID and a description (showing the description). I have been able to display the combo but I haven't been able to retreive the selected item from the combo. Also, my combo is only filled when the editor is required (CellBeginEdit) which means that the description is not shown as long as I don't edit that cell.

    Do you have an example that would be good for me?
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 07 Dec 2010 Link to this post

    Hello,

    First thing, because you are dealing with a text column you cannot define an Id in the combo, you will have to define both the ValueMember and the DisplayMember to your description, but this will lead to a new problem, one i have solved here using a BindingSource and Linq.

    Please take a look at the following example:
    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(radGridView1 = new RadGridView());
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.Dock = DockStyle.Fill;
            radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
            radGridView1.EditorRequired += new EditorRequiredEventHandler(radGridView1_EditorRequired);
        }
     
        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (radGridView1.CurrentCell.ColumnInfo.Name == "Buyer")
            {
                e.EditorType = typeof(RadDropDownListEditor);
                e.Editor = new RadDropDownListEditor();
            }
        }
     
        void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            var dropDownEditor = e.ActiveEditor as RadDropDownListEditor;
            if (dropDownEditor == null)
            {
                return;
            }
     
            var element = dropDownEditor.EditorElement as RadDropDownListEditorElement;
     
            var bindingSource = new BindingSource();
            var buyers = new BuyersCollection();
            bindingSource.DataSource = buyers;
            bindingSource.Position =
                buyers.IndexOf(buyers.Where(buyer => buyer.Name == this.radGridView1.CurrentCell.Value.ToString()).FirstOrDefault());
            element.DataSource = bindingSource;
     
            element.DisplayMember = "Name";
            element.ValueMember = "Name";
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            radGridView1.DataSource = new ProductsCollection();
        }
    }
     
    public class Product
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Buyer
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public Product(int id, string buyer, string name)
        {
            this.Id = id;
            this.Buyer = buyer;
            this.Name = name;
        }
    }
     
    public class Buyer
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public Buyer(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
     
    public class ProductsCollection : BindingList<Product>
    {
        public ProductsCollection()
        {
            for (int i = 0; i < 10; i++)
            {
                this.Add(new Product(i, "Buyer" + i, "Product" + i));
            }
        }
    }
     
    public class BuyersCollection : BindingList<Buyer>
    {
        public BuyersCollection()
        {
            for (int i = 0; i < 10; i++)
            {
                this.Add(new Buyer(i, "Buyer" + i));
            }
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Eric Moreau
    Eric Moreau avatar
    22 posts
    Member since:
    Sep 2007

    Posted 08 Dec 2010 Link to this post

    Hi

    Thanks for your comment but it doesn't really help me. I have 2 columns (ID and Description). 2 specific questions:

    1. When the grid is loaded, I see the IDs. Is there anyway to fill the cell with the combos instead of waiting for the EditorRequired event to load it (my grid only contains a dozen of rows)?

    2. How to retreive the ID of the selected item?
  5. Eric Moreau
    Eric Moreau avatar
    22 posts
    Member since:
    Sep 2007

    Posted 21 Dec 2010 Link to this post

    How come this comment is marked as an answer while it doesn't fit my needs? It is misleading to anybody who will have the same question.

    Anyway, using other source, I have been able to do something good.
  6. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 27 Dec 2010 Link to this post

    Hi Eric Moreau,

    Regarding your questions:

    1. Yes this is possible, you have to use custom cell elements like described in this KB article. However, this can lead performance issues and visual glitches. The combo boxes are controls and RadGridView cannot clip them when scrolling. We do not recommend this option, so please describe in detail your case and we will try to find the best option.

    2. I am not sure that I fully understand the question. Please, could you send me your application. This will help me to understand the issue and find a proper solution.

    I am looking forward to your reply.

    Kind regards, Julian Benkov
    the Telerik team
    Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
Back to Top
UI for WinForms is Visual Studio 2017 Ready