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

GridViewLookUpColumn with Datasource as Custom Business Object

3 Answers 118 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Fuat
Top achievements
Rank 1
Fuat asked on 23 Apr 2010, 05:42 PM
Hi,

I am trying Telerik's wonderful components for my next project, but there is only a problem for now that hasn't figured out yet.

Think about 2 business objects;

Product
Int32 - id
String - name
Category - category

Category
Int32 - id
Strıng - name

How to use Gridview component to add and edit new Products and use GridViewLookUpColumn to select Category for that product.

I tried Gridview columns ID,name,category.name and it shows correct name of category. But when I want to change the product's category from pre filled datasource resCategories, after selection, there appears an error says, cannot convert string to Category, which is because of it is trying to set category name to product.category field. But how to do it in correct way.

Sincerely,
Fuat AKGÜN

3 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 29 Apr 2010, 10:15 AM
Hi Fuat Akgün,

The RadGridView control can not support this functionality built-in. Here is the solution:

public partial class Form1 : Form
{
    public class Product
    {
        public int Id
        {
            get;
            set;
        }
 
        public string Name
        {
            get;
            set;
        }
 
        public Category Category
        {
            get;
            set;
        }
    }
 
    public class Category
    {
        public int Id
        {
            get;
            set;
        }
 
        public string Name
        {
            get;
            set;
        }
    }
 
    private GridViewComboBoxColumn comboColumn;
 
    public Form1()
    {
        InitializeComponent();
        this.radGridView1.ValueChanging += new ValueChangingEventHandler(radGridView1_ValueChanging);
 
        BindingList<Category> categories = new BindingList<Category>();
        Category cat = new Category();
        cat.Id = 0;
        cat.Name = "PC";
        categories.Add(cat);
        cat = new Category();
        cat.Id = 1;
        cat.Name = "Laptop";
        categories.Add(cat);
        cat = new Category();
        cat.Id = 2;
        cat.Name = "Phone";
        categories.Add(cat);
 
        BindingList<Product> products = new BindingList<Product>();
        Product product = new Product();
        product.Id = 0;
        product.Name = "IBM";
        product.Category = categories[0];
        products.Add(product);
        product = new Product();
        product.Id = 1;
        product.Name = "HP";
        product.Category = categories[1];
        products.Add(product);
        product = new Product();
        product.Id = 2;
        product.Name = "HTC";
        product.Category = categories[2];
        products.Add(product);
 
        this.radGridView1.DataSource = products;
        this.radGridView1.Columns.RemoveAt(2);  //remove auto-generated Category column
        this.comboColumn = new GridViewComboBoxColumn();
        comboColumn.DataType = typeof(string);
        comboColumn.DataSource = categories;
        comboColumn.DisplayMember = "Name";
        comboColumn.FieldName = "Category.Name";
        this.radGridView1.Columns.Add(comboColumn);
    }
 
    void radGridView1_ValueChanging(object sender, ValueChangingEventArgs e)
    {
        RadComboBoxEditor editor = this.radGridView1.ActiveEditor as RadComboBoxEditor;
        if (editor != null && this.radGridView1.CurrentColumn == this.comboColumn)
        {
            RadComboBoxEditorElement element = editor.EditorElement as RadComboBoxEditorElement;
            RadComboBoxItem item = element.SelectedItem as RadComboBoxItem;
            Category cat = item.DataItem as Category;
            if (cat != null)
            {
                Product product = this.radGridView1.CurrentRow.DataBoundItem as Product;
                product.Category = cat;
            }
 
            e.Cancel = true;
            this.radGridView1.CurrentRow.InvalidateRow();
        }
    }
}

A bit off topic, please ask the person who has purchased our controls in your company to add you as a License Developer to the purchase. This will give you full access to the products your company has purchased, to our downloads section, and to our support ticketing system. Additionally, all your questions will be reviewed according to the license you have. More information on License Developers you can find here: www.telerik.com/account/faqs.aspx.

All the best,

Julian Benkov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Fuat
Top achievements
Rank 1
answered on 29 Apr 2010, 04:30 PM
Thanks for this wonderful example. It will be very useful.

We are just trying the products to choose the best for our next project and I am sure its going to be Telerik.

Sincerely,
Fuat AKGÜN
0
Nikolay
Telerik team
answered on 05 May 2010, 10:10 AM
We are glad to be of help, Fuat. Let us know if you need additional assistance.

Kind regards,
Nikolay
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
Fuat
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Fuat
Top achievements
Rank 1
Nikolay
Telerik team
Share this question
or