Problem with init GridViewMultiComboBoxColumn

3 posts, 1 answers
  1. flisak
    flisak avatar
    6 posts
    Member since:
    Jan 2012

    Posted 16 Mar 2012 Link to this post

    Hi,

    I have problem with GridViewMultiComboBoxColumn. I using the latest version of telerik package (Q1 2012).

    I have two objects:

    public class Offer
    {
      public Guid Id { get; set; }
      public string OfferNo { get; set; }
      public Customer Client { get; set; }
      public DateTime? DateFrom { get; set; }
      public DateTime? DateTo { get; set; }
    }
     
    public class Customer
    {
      public Guid CustomerId { get; set; }
      public string Name { get; set; }
      public string ShortName { get; set; }
    }

    When i set GridViewMultiComboBoxColumn for "Client" property then I have exception: Converter can't convert.
    If I dont't set ValuMember property, then is no exception, byt choose object in grid don't work.

    public Form1()
    {
        InitializeComponent();
     
        custs = GridData.GetCustomers(5);
     
        IList offers = GridData.GetGridData(custs);
     
        radGridView1.DataSource = offers;
        SetColumns();
    }
     
    private void SetColumns()
    {
        radGridView1.Columns.Clear();
     
        GridViewDataColumn column = null;
        for (int i = 0; i < 4; i++)
        {
            switch (i)
            {
                case 0:
                    column = new GridViewTextBoxColumn("Offer no");
                    break;
                case 2:
                    column = new GridViewDateTimeColumn("Date from");
                    break;
                case 3:
                    column = new GridViewDateTimeColumn("Date to");
                    break;
                case 1:
                    column = new GridViewMultiComboBoxColumn("Client");
                    ((GridViewMultiComboBoxColumn)column).FieldName = "Client";                       
                    ((GridViewMultiComboBoxColumn)column).DataSource = custs;
                    ((GridViewMultiComboBoxColumn)column).DisplayMember = "Name";
                    ((GridViewMultiComboBoxColumn)column).ValueMember = "CustomerId";
                    break;
            }
     
            column.Width = 150;
            column.IsVisible = true;
            column.ReadOnly = false;
     
            radGridView1.Columns.Add(column);
        }
    }

    What am I doing wrong?

    Best Regards
  2. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 19 Mar 2012 Link to this post

    Hello Krzysztof,

    You are trying to bind a custom object to a GridViewMultiCoboBoxColumn. The RadGridView for WinForms does not support that kind of binding out of the box. You should use sub-property binding with combination of CellValidating event in the following way:
    GridViewMultiComboBoxColumn column = new GridViewMultiComboBoxColumn("Client.CustomerId");
    column.DataSource = custs;
    column.DisplayMember = "Name";
    column.ValueMember = "CustomerId";
    this.radGridView1.Columns.Add(column);

    private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
    {
        RadMultiColumnComboBoxElement mccb = e.ActiveEditor as RadMultiColumnComboBoxElement;
     
        if (mccb != null && e.Column.Name == "Client.CustomerId")
        {
            e.Cancel = true;
            Offer offer = e.Row.DataBoundItem as Offer;
            offer.Client = mccb.EditorControl.CurrentRow.DataBoundItem as Customer;
        }
    }

    In addition, you should set the CloseEditorWhenValidationFails property of RadGridView to true:
    this.radGridView1.CloseEditorWhenValidationFails = true;

    I hope this helps.

    Greetings,
    Svett
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. flisak
    flisak avatar
    6 posts
    Member since:
    Jan 2012

    Posted 20 Mar 2012 Link to this post

    Thanks Svett. This works fine!

    Regards
Back to Top