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

Filter inactive values from being selected

7 Answers 217 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Jeremie
Top achievements
Rank 1
Jeremie asked on 01 Dec 2011, 05:01 PM
Hello,

We have a scenario where we have a table of values that for example list what position type a certain contact would be, the table has an active flag that when set to 'N' means we do not want this value selected on any new entries.  However there is a high possibility that existing records may still use that value.  So when a user opens that contact they should still see that contact's position type, but they shouldn't be able to select an inactive value.

Is it possible to achieve this with the drop down list?

Thanks,

Jeremie

7 Answers, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 05 Dec 2011, 01:59 PM
Hi Jeremie,

Thank you for writing.

This scenario is certainly achievable. The easiest way to achieve it is to set the Enabled property, of the items you don't want to be selectable, to false. You can use the VisualListItemFormatting event to determine which items to disable. Here is a code snippet which demonstrates this:

private void radDropDownList1_VisualListItemFormatting(object sender, VisualItemFormattingEventArgs args)
{
  args.VisualItem.Enabled = ((MyPositionType)args.VisualItem.Data.DataBoundItem).Flag == Flags.N;
}

I hope this will help you. Should you have further questions, I would be glad to help. Kind regards,
Ivan Petrov
the Telerik team

Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

0
Jeremie
Top achievements
Rank 1
answered on 05 Dec 2011, 04:10 PM
Ivan,

Thanks for your response, this is exactly what I was hoping for, almost.

While the items indeed display as though they are disabled, if the user presses and up and down arrows to go through the list they are still selectable.

I also tried setting the Visibility property to both ElementVisibility.Collapsed and ElementVisibility.Hidden however this creates gaps in the drop down list. See the attached image.

Thanks,

Jeremie Grund
0
Ivan Petrov
Telerik team
answered on 06 Dec 2011, 03:46 PM
Hi Jeremie,

Thank you for your reply.

I have made a small mistake in the code snippet from my previous post. I used the Enabled property of the visual item and not of the actual data item. This way when navigating through the items collection all items would be selectable as none is disabled. Here is the correct snippet which should work for you:

private void radDropDownList1_VisualListItemFormatting(object sender, VisualItemFormattingEventArgs args)
{
  args.VisualItem.Data.Enabled = ((MyPositionType)args.VisualItem.Data.DataBoundItem).Flag == Flags.N;
}

I hope this will solve the issue. If you need further assistance, I would be glad to provide it.
Kind regards,
Ivan Petrov
the Telerik team

Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

0
Jeremie
Top achievements
Rank 1
answered on 06 Dec 2011, 04:34 PM
Ivan,

Thanks for the feedback.  I tried this change and it appears to have the same affect.  Inactive items in the drop down appear gray but when using the keyboard they are still selectable.

Thanks,

Jeremie Grund
0
Ivan Petrov
Telerik team
answered on 08 Dec 2011, 02:21 PM
Hi Jeremie,

Thank you for writing back.

I noticed where my solution was failing. It is when you do not open the drop down, which means that the event is never fired and no items are disabled. To make sure all cases are covered you should disable the items you want directly after you have added them to the drop down list. If you are adding them manually you should set the Enabled property of each item when you create it. If you are binding the drop down list to some data source you will have to iterate over all the items and set the Enabled property after you set the drop down list data source.

I hope this will cover all cases. Feel free to write back with any further questions.

Regards,
Ivan Petrov
the Telerik team

Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

0
Giang
Top achievements
Rank 1
answered on 07 Dec 2017, 08:46 AM
How to set items are disabled if I load data for DropDownList by data source?

Thank you.
0
Hristo
Telerik team
answered on 11 Dec 2017, 12:02 PM
Hi Giang,

Thank you for writing.

Assuming you have already bound the drop-down list, you can iterate the Items collection, access the data-bound object and disable the list item:
protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
 
    foreach (RadListDataItem item in this.radDropDownList1.Items)
    {
        object dataBound = item.DataBoundItem;
        //Validate the data bound item and if necessary disabled the list item in the drop-down
        item.Enabled = false;
    }
}

An alternative approach is to handle the ItemDataBound event  of the control and set the Enabled property there: 
private void RadDropDownList1_ItemDataBound(object sender, ListItemDataBoundEventArgs args)
{
    //DataModel is the object used to bind the control
    args.NewItem.Enabled = ((DataModel)args.NewItem.DataBoundItem).Id % 2 == 0;
}

I hope this helps. Let me know if you need further assistance.

Regards,
Hristo
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.
Tags
DropDownList
Asked by
Jeremie
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
Jeremie
Top achievements
Rank 1
Giang
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or