MultiColumn ComboBox - Selected Value

6 posts, 0 answers
  1. Martin
    Martin avatar
    35 posts
    Member since:
    Jun 2014

    Posted 05 Mar 2015 Link to this post

    I have a multi-column combo box which is populated via a LINQ query to an entity in my context

     //Grab the data from the table specified in the Model project
                        var query = from i in db.ItemTypes.AsNoTracking()
                                    where i.Active == active
                                    orderby i.Code

                                    //This allows a switch between the ITNBR and the Description
                                    select new { i.ID, i.Code, i.Description };

                        if (query.Count() > 0)
                        {
                            //Now we can bind the results to the control
                            cbo.DataSource = query.ToList();
                            cbo.ValueMember = "ID";
                        }

    Which works fine and the data is populated correctly. I have used similar to populate other multicolumn combos elsewhere too.

    As the form is instanciated the selected index is set to -1
    The problem is when I go radMultiColumnComboBoxItemType.SelectedValue = 3 the SelectedValue still = null.

    Any ideas folks?
    Thanks.

  2. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 06 Mar 2015 Link to this post

    Hi Martin,

    Thank you for writing.

    Despite my efforts I was unable to replicate such a behavior on my end, using our latest version. Here is a small app I setup:
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
     
        List<Person> people = new List<Person>();
       
        for (int i = 0; i < 50; i++)
        {
            people.Add(new Person() { Age = i, Name = "Name " + i });
        }
     
        this.radMultiColumnComboBox1 = new RadMultiColumnComboBox();
        this.radMultiColumnComboBox1.Size = new System.Drawing.Size(300, 20);
        this.Controls.Add(this.radMultiColumnComboBox1);
     
        var query = from person in people
                    where person.Age > 18 && person.Age < 25
                    select person;
     
        radMultiColumnComboBox1.DataSource = query.ToList();
        radMultiColumnComboBox1.ValueMember = "Name";
    }
     
    class Person
    {
        public int Age { get; set; }
        public string Name { get; set; }
    }

    Once I start this, the SelectedValue == "Name 19" (which is the first item in the resulted list). If I change it, it changes accordingly.

    Can you please modify my sample on a way to replicate the undesired behavior and get back to me with it so I can investigate the precise case?

    I am looking forward to your reply.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Martin
    Martin avatar
    35 posts
    Member since:
    Jun 2014

    Posted 06 Mar 2015 in reply to Stefan Link to this post

    Hi Stefan,
    It seems to happen on some controls and not others. I am also having the same issue on radDropDownList. It must be something I am doing because on some of the dropdowns it works fine.

    I have an object which returns values to the form.

    In the object:

    public void FetchAll(RadDropDownList cbo, Boolean active)
            {
                try
                {
                    //Ensure that the dropdown is blanked out prior to population
                    cbo.DataSource = null;

                    using (var db = new DBContext())
                    {
                        //Grab the data from the table specified in the Model project
                        var query = from c in db.CurrencyTypes.AsNoTracking()
                                    where c.Active == active
                                    orderby c.Code

                                    //This allows a switch between the ITNBR and the Description
                                    select new { c.ID, cboText = c.Code + "-" + c.Description };

                        if (query.Count() > 0)
                        {
                            //Now we can bind the results to the control
                            cbo.DataSource = query.ToList();
                            cbo.DisplayMember = "cboText";
                            cbo.ValueMember = "ID";
                        }
                    }
                }
                catch (Exception ex)
                {
                    GlobalErrorHandler.CallingRoutine = MethodBase.GetCurrentMethod().Name;
                    GlobalErrorHandler.CallingModule = typeof(PresenterCurrencyType).ToString();
                    GlobalErrorHandler.Exception = ex.Message.ToString();
                    GlobalErrorHandler.HandleError();
                }
            }

    So the dropdown list is populated with ID (PK of Table), Code and Description. And it is populated fine.

    When I want to 'Fetch' Data I return an object to the form:

    public Model.VendMast Refresh()
            {
                using (var db = new CertEaseContext())
                {
                    var vend = new VendMast();

                    try
                    {
                        var exists = db.VendMasts.Count(a => a.ID == _id);
                        if (exists == 1)
                        {
                            var row = db.VendMasts.First(r => r.ID == _id);
                            if (row != null)
                                vend = row;
                        }
                    }
                    catch (Exception ex)
                    {
                        GlobalErrorHandler.CallingRoutine = MethodBase.GetCurrentMethod().Name;
                        GlobalErrorHandler.CallingModule = typeof(PresenterVendor).ToString();
                        GlobalErrorHandler.Exception = ex.Message.ToString();
                        GlobalErrorHandler.HandleError();
                    }

                    //Return value
                    return vend;
                }
            }

    Then on the form:

    vend.ID = Convert.ToInt32(radDropDownListVendorAll.SelectedValue);
    ClearControls();

    var obj = vend.Refresh();
    if (obj.AccountNo != null) radTextBoxAccountNo.Text = obj.AccountNo.Trim().ToString();
    if (obj.VendNo != null) radTextBoxVendorNo.Text = obj.VendNo.Trim().ToString();
    if (obj.Name != null) radTextBoxVendorName.Text = obj.Name.Trim().ToString();
    radDropDownListTermCodeType.SelectedValue = obj.TermCodeTypeID;
    radDropDownListCurrencyType.SelectedValue = obj.CurrencyID;
    radDropDownListSuppType.SelectedValue = obj.SupplierTypeID;
    radCheckBoxActive.Checked = obj.Active;

    radDropDownListTermCodeType WORKS - the other two the SelectedValue stays as NULL. I've tried casts on the values returned too. 

    I know there is a lot to look at here but it must be something simple that I am missing.
    thanks!

  5. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 10 Mar 2015 Link to this post

    Hi Martin,

    Thank you for writing.

    With this setup, the drop downs will contain items and each item Value will be whatever is contained in the following property:
    var query = from c in db.CurrencyTypes.AsNoTracking()
     where c.Active == active
     orderby c.Code
     
     //This allows a switch between the ITNBR and the Description
     select new { c.ID, cboText = c.Code + "-" + c.Description };

    So, if you assign the SelectedValue with a valid ID, contained in the control it should get selected. I have tested this with the following example:
    RadDropDownList ddl = new RadDropDownList();
     
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
     
        ddl.Parent = this;
     
        FetchAll(ddl, 20);
    }
     
    public void FetchAll(RadDropDownList cbo, int min)
    {
        //Ensure that the dropdown is blanked out prior to population
        cbo.DataSource = null;
     
        List<Person> people = new List<Person>();
     
        for (int i = 0; i < 50; i++)
        {
            people.Add(new Person() { ID = i, Code = i * 10, Description = "Desc " + i });
        }
     
        var query = from person in people
                    where person.ID > min
                    select new { person.ID, cboText = person.Code + "-" + person.Description};
     
        if (query.Count() > 0)
        {
            //Now we can bind the results to the control
            cbo.DataSource = query.ToList();
            cbo.DisplayMember = "cboText";
            cbo.ValueMember = "ID";
        }
    }
     
    class Person
    {
        public int ID { get; set; }
        public int Code { get; set; }
        public string Description { get; set; }
    }
     
     
    private void radButton1_Click(object sender, EventArgs e)
    {
        ddl.SelectedValue = 30;
    }

    I cannot think of a reason for the drop down's to have its SelectedValue == null, if a proper ID is assigned. 

    Should you continue to experience this undesired behavior I would kindly ask you to create a small sample project where the issue can be reproduced and get back to me with it, so I can investigate the precise case and help you resolve it. In addition, please include a description of the steps that I need to take in order to reproduce the experienced issue. 

    I am looking forward to your reply.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Martin
    Martin avatar
    35 posts
    Member since:
    Jun 2014

    Posted 13 Mar 2015 in reply to Stefan Link to this post

    Hi Stefan,
    I managed a work-around solution by setting the selected index property in a static function:

    public static void radDropDownSetValue(RadDropDownList cbo, Int64 id)
            {
                int index = 0;

                try
                {
                    if (id > 0)
                    {
                        for (index = 0; index < cbo.Items.Count; index++)
                        {
                            if (Convert.ToInt64(cbo.Items[index].Value) == id)
                            {
                                cbo.SelectedIndex = index;
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    GlobalErrorHandler.CallingRoutine = MethodBase.GetCurrentMethod().Name;
                    GlobalErrorHandler.CallingModule = typeof(GlobalControlMethods).ToString();
                    GlobalErrorHandler.Exception = ex.Message.ToString();
                    GlobalErrorHandler.HandleError();
                }
            }

    This overcomes my problem and I can set any radDropDownList to the desired value
    Thanks,
    Martin.
  7. Dess
    Admin
    Dess avatar
    1607 posts

    Posted 17 Mar 2015 Link to this post

    Hello Martin,

    Thank you for writing back.

    I am glad that you have found a suitable solution for your specific case. However, if you manage to replicate the described problem with a sample code example it would be highly appreciated if you get back to us with it. Thus, we would be able to investigate the precise case and give you and adequate explanation what is causing selection problem with RadMultiColumnComboBox. Thank you in advance.

    If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for WinForms is Visual Studio 2017 Ready