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

MultiColumn ComboBox - Selected Value

8 Answers 369 Views
MultiColumn ComboBox
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 05 Mar 2015, 03:58 PM
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.

8 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 06 Mar 2015, 02:44 PM
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.

 
0
Martin
Top achievements
Rank 1
answered on 06 Mar 2015, 04:49 PM
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!

0
Stefan
Telerik team
answered on 10 Mar 2015, 10:44 AM
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.

 
0
Martin
Top achievements
Rank 1
answered on 13 Mar 2015, 02:12 PM
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.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Mar 2015, 12:52 PM
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.

 
0
Chris Kirkman
Top achievements
Rank 1
answered on 20 Mar 2018, 08:24 PM

I've had an incredibly hard time trying to do a simple assignment to a row in this control.  Although I'd databound a BindingList<myobject> to it, there was nothing I could do to set it to a specific row that I wanted via code behind.  I finally landed on looping through row by row, by index, checking the databound item for each row until I found the one that matched my criteria.  Only then did I set the Multicolumn ComboBox by "SelectedIndex".  After 3 hours this was the only thing that would work.  I tried ComboBox.SelectedValue, ComboBox.SelectedItem, ComboBox..MultiColumnComboBoxElement.SelectedValue, ComboBox.MultiColumnComboBoxElement.SelectedItem all to no avail.  Here's my code and maybe you can make something of it, but I couldn't ever get it to work the way I would have expected, so I just moved on.  :(

In my constructor...
 
            _model = new FieldsViewModel(deliverySystem.Fields);
            _ray = ray;
 
            FieldSizeComboBox.ValueMember = "Id";
            FieldSizeComboBox.DataSource = _model.Fields;
 
 
In form load...
        private void RayTraceSetupForm_Load(object sender, System.EventArgs e)
        {
            // set values
            if (null != _ray)
            {
                // field size
                FieldViewModel m = _model.Fields.First(f => f.Id == _ray.FieldSize.FieldId);
 
                for(int i = 0; i < FieldSizeComboBox.MultiColumnComboBoxElement.Rows.Count; i++)
                {
                    GridViewRowInfo row = FieldSizeComboBox.MultiColumnComboBoxElement.Rows[i];
 
                    FieldViewModel model = row.DataBoundItem as FieldViewModel;
                    if (model.Id == m.Id)
                    {
                        FieldSizeComboBox.MultiColumnComboBoxElement.SelectedIndex = i;
                    }
                }
        }
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Mar 2018, 09:17 AM
Hello, Chris, 

According to the provided code snippet I suppose that you are trying to set a specific value to RadMultiColumnComboBox. For this purpose, it is necessary to set the SelectedValue property to a valid value according to the specified RadMultiColumnComboBox.ValueMember property. 
DataTable dt = new DataTable();
dt.Columns.Add("Id",typeof(int));
dt.Columns.Add("Name",typeof(string));
for (int i = 0; i < 10; i++)
{
    dt.Rows.Add(i,"Item"+i);
}
 
this.radMultiColumnComboBox1.DataSource = dt;
this.radMultiColumnComboBox1.DisplayMember = "Name";
this.radMultiColumnComboBox1.ValueMember = "Id";
this.radMultiColumnComboBox1.SelectedValue = 4;



I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Chris Kirkman
Top achievements
Rank 1
answered on 27 Mar 2018, 02:24 PM
This whole time I've been trying to bind to a ValueMember that didn't exist in my Grid.  "Id" was my ValueMember; however, since I had pre-defined my columns and "Id" wasn't one of them the binding never worked.  This is my fault.  :(  I just added the "Id" column and set it to not visible and my problem.  Went away.  Thanks for the help regardless.
Tags
MultiColumn ComboBox
Asked by
Martin
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Martin
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Chris Kirkman
Top achievements
Rank 1
Share this question
or