RadComboBox – load even if value is missing in dataset

In developing a web application, I’ve frequently run into an issue with the dataset for a Radcombobox not being equal to historical data in the database.

For example, a table that stores equipment, with a column of employee numbers representing “assigned to”.

In the web application, I only want users to select Active employees so this is the contents of my Radcombobox dataset.

However, employees frequently transition to Inactive as they leave the company and there may still be equipment assigned to them. If they are marked Inactive and the Radcombobox is loaded for that record, it will error out with “Selection out of range Parameter name“.

Luckily I found this exact same situation on the Telerik forums, and the last post in the thread is what I needed.

In my case I have validation for empty Assigned Employees, so I modified the function to look like this:

protected void PreventErrorOnbinding(object sender, EventArgs e)
        {
            RadComboBox cb = sender as RadComboBox;
            cb.DataBinding -= new EventHandler(PreventErrorOnbinding);
            cb.AppendDataBoundItems = true;

            try
            {
                cb.DataBind();
                //cb.Items.Clear(); // Don't clear the combobox if the Databind is successful
            }
            catch (ArgumentOutOfRangeException)
            {
                cb.Items.Clear();
                cb.ClearSelection();
                RadComboBoxItem cbI = new RadComboBoxItem("", "");
                cbI.Selected = true;
                cb.Items.Add(cbI);
            }
        }

Then in my Radcombobox, I added “OnDataBinding="PreventErrorOnbinding".

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.