ComboboxColumn in radgridview with objects

7 posts, 0 answers
  1. Erik
    Erik avatar
    4 posts
    Member since:
    Oct 2016

    Posted 26 Oct Link to this post

    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.

  2. Erik
    Erik avatar
    4 posts
    Member since:
    Oct 2016

    Posted 26 Oct Link to this post

    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.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 26 Oct Link to this post

    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.
  5. Erik
    Erik avatar
    4 posts
    Member since:
    Oct 2016

    Posted 26 Oct in reply to Dimitar Link to this post

    Works great but now i have orders that are not "assigned". They should also appear in the combobox.
  6. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 27 Oct Link to this post

    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.
  7. Erik
    Erik avatar
    4 posts
    Member since:
    Oct 2016

    Posted 27 Oct in reply to Dimitar Link to this post

    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,

  8. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 28 Oct Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready