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
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.
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
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.
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
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.
Ivan Petrov
the Telerik team
Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
Thank you.
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