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

custom cell editing

4 Answers 147 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Eric Moreau
Top achievements
Rank 2
Iron
Iron
Veteran
Eric Moreau asked on 07 Dec 2010, 08:10 PM
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?

4 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 07 Dec 2010, 09:48 PM
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
0
Eric Moreau
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 09 Dec 2010, 03:00 AM
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?
0
Eric Moreau
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 21 Dec 2010, 03:03 PM
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.
0
Julian Benkov
Telerik team
answered on 27 Dec 2010, 06:46 PM
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.
Tags
GridView
Asked by
Eric Moreau
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Emanuel Varga
Top achievements
Rank 1
Eric Moreau
Top achievements
Rank 2
Iron
Iron
Veteran
Julian Benkov
Telerik team
Share this question
or