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

ComboboxColumn in radgridview with objects

6 Answers 151 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Erik
Top achievements
Rank 1
Erik asked on 26 Oct 2016, 07:40 AM

I seem to have a problem with changing my datasource.

I have 2 Objects: One with InvoiceLines made up of the following properties: int id, int index, string name and Order order

My Order object is made up of the following properties: int id and string name.

The goal is to be able to select, per InvoiceLine, a corresponding Order by selecting an Order with a combobox. the comboboxcolumn is created via this code: 

gvTest.Columns.Add(new GridViewComboBoxColumn { HeaderText = "Order name", FieldName = "Order", DataSource = orderLines, DisplayMember = "Order.Name", ValueMember = ""});

problem is that when running the form, in the Combobox column, the first thing showed is: GridViewTets.Models.Order (which should be the name of the Order) and when selecting another object Order in the combobox and confirming the edit an errorbox shows with this message translated from dutch: an object of type System.String cannot be cast to type GridViewTest.Models.Order.

6 Answers, 1 is accepted

Sort by
0
Erik
Top achievements
Rank 1
answered on 26 Oct 2016, 11:58 AM

An easier way to explain all this is: I want to change my valuemember to an object in the comboboxcolumn datasource, not the property of an object.

0
Dimitar
Telerik team
answered on 26 Oct 2016, 12:15 PM
Hi Erik,

I have created a small example that shows how you can implement this:
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();
        BindingList<InvoiceLines> data = new BindingList<InvoiceLines>();
        for (int i = 0; i < 10; i++)
        {
            var obj = new InvoiceLines() { Id = i, Index = i, Name = "Name" };
            obj.order = new Order { Id = i, Name = "Name " + i };
            data.Add(obj);
        }
        radGridView1.DataSource = data;
 
     
 
        var comboColumn = new GridViewComboBoxColumn();
        comboColumn.Name = "CustomOrderColumn";
        comboColumn.Width = 100;
        comboColumn.FieldName = "Order";
        comboColumn.DisplayMember = "order.Name";
        comboColumn.ValueMember = "order";
        comboColumn.DataSource = data;
 
        radGridView1.Columns.Add(comboColumn);
     
    }
 
     
}
 
class InvoiceLines
{
    public int Id { get; set; }
    public int Index { get; set; }
    public string Name { get; set; }
 
    public Order order { get; set; }
 
 
}
class Order : IComparable
{
    public int Id { get; set; }
    public string Name { get; set; }
 
    public int CompareTo(object obj)
    {
        return Name.CompareTo(((Order)obj).Name);
    }
 
    public override string ToString()
    {
        return Name;
    }
}

I hope this will be useful. Let me know if you have additional questions.

Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Erik
Top achievements
Rank 1
answered on 26 Oct 2016, 12:55 PM
Works great but now i have orders that are not "assigned". They should also appear in the combobox.
0
Dimitar
Telerik team
answered on 27 Oct 2016, 08:56 AM
Hi Erik,

I am not sure what exactly you have meant by "assigned". Could you please elaborate?

Perhaps you can use separate data sources for the grid and the column. 

Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Erik
Top achievements
Rank 1
answered on 27 Oct 2016, 09:00 AM

Hi Dimitar,

What I mean is i can have 10 Orders but only 2 InvoiceLines and would have to be able to select an order when making an invoiceline which is not assigned yet to an InvoiceLIne yet. In the example you gave me I am unable to do this. And when using different datasources I am unable to set the value to orders.

Kind regards,
Erik Stevens,

0
Dimitar
Telerik team
answered on 28 Oct 2016, 07:34 AM
Hi Erik,

I have extended the example and now it uses two data sources and there are orders that are not yet assigned. In addition, I have added a filter so only the "unassigned" orders are appearing in the drop down. 

I hope this will be useful. 

Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Tags
GridView
Asked by
Erik
Top achievements
Rank 1
Answers by
Erik
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or