Filter inactive values from being selected

6 posts, 0 answers
  1. Jeremie
    Jeremie avatar
    13 posts
    Member since:
    Apr 2011

    Posted 01 Dec 2011 Link to this post

    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
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 05 Dec 2011 Link to this post

    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.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jeremie
    Jeremie avatar
    13 posts
    Member since:
    Apr 2011

    Posted 05 Dec 2011 Link to this post

    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
  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 06 Dec 2011 Link to this post

    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.

  6. Jeremie
    Jeremie avatar
    13 posts
    Member since:
    Apr 2011

    Posted 06 Dec 2011 Link to this post

    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
  7. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 08 Dec 2011 Link to this post

    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.

Back to Top
UI for WinForms is Visual Studio 2017 Ready