none
Can I make a ComboBox display the ValueMember instead of the DisplayMember? RRS feed

  • Question

  • I have a ComboBox with a DataSource, DisplayMember, and ValueMember.  And all works well except that when the drop down list is closed I'd like the ValueMember displayed.  That must sound strange but I have space constraints so I want full names of things to appear in the drop down list but then want to display only the value member, which is an abbreviation, when the drop down list is closed.  I actually make the control wider when the list drops down, and then restore it to its original width in the DropDownClosed event handler.

    I've tried setting the Text member in both DropDownClosed and SelectedIndexChanged but neither approach worked.

    And ideas?

    Thanks,  Bob

    Thursday, August 21, 2014 10:48 PM

Answers

  • That is indeed a bizarre requirement.  I don't think that is easily doable using the automatic binding that the control is going to use.  Whenever it needs to render the control it is going to use the value from DisplayMember.  Even if you handled the various events it would still revert when the control repaints itself.

    I think there are a couple of solutions to this problem but none of them are elegant.  One option might be to use autocompletion instead of a bound dropdown list.  You can get similar behavior but you can alter the drop down list more dynamically.  You might still need to muck with the closing of the drop down to get the right text to show up.  The default behavior is to call ToString() so what you show in the auto complete list could (theoretically) be different then that.

    Another option might be to bind the drop down to the version you want to see while collapsed.  When the DropDown event occurs rebind the control to an altered version that shows the full name.  Rebind back on DropDownClosed.

    The final option is to forego the combo and use a different control altogether.  For example you could show a simple read only textbox and require that the user click some sort of change button to change the value.  Alternatively you could show a hyperlink that, when clicked, shows a drop down list with the values to choose from (or a context menu if the list is small).

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Friday, August 22, 2014 5:43 PM
    Moderator

All replies

  • Hello,

    >>Can I make a ComboBox display the ValueMember instead of the DisplayMember?

    What project type are you working with?

    If you are working with windows form, you could post it to:

    http://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms

    If you are using WPF, you could post it to WPF fourm:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    If you are working with a web application, you could post it to the ASP.NET forum:

    http://forums.asp.net/

    This forum is for discussing and asking questions about .NET Framework Base Classes.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 22, 2014 8:15 AM
    Moderator
  • That is indeed a bizarre requirement.  I don't think that is easily doable using the automatic binding that the control is going to use.  Whenever it needs to render the control it is going to use the value from DisplayMember.  Even if you handled the various events it would still revert when the control repaints itself.

    I think there are a couple of solutions to this problem but none of them are elegant.  One option might be to use autocompletion instead of a bound dropdown list.  You can get similar behavior but you can alter the drop down list more dynamically.  You might still need to muck with the closing of the drop down to get the right text to show up.  The default behavior is to call ToString() so what you show in the auto complete list could (theoretically) be different then that.

    Another option might be to bind the drop down to the version you want to see while collapsed.  When the DropDown event occurs rebind the control to an altered version that shows the full name.  Rebind back on DropDownClosed.

    The final option is to forego the combo and use a different control altogether.  For example you could show a simple read only textbox and require that the user click some sort of change button to change the value.  Alternatively you could show a hyperlink that, when clicked, shows a drop down list with the values to choose from (or a context menu if the list is small).

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Friday, August 22, 2014 5:43 PM
    Moderator
  • Thanks for responding Michael.  It may be insanity but I think that I will try my own user control.  If I needed something with all the functionality of ComboBox that would be total insanity.  But I need something rather simply.  A drop down which displays full text and abbreviations, text strings only, and then after the selection is made it will display only the abbreviation.  I won't inherit ComboBox, I'll start from scratch.  I think that it sounds pretty doable.

    Thanks again,  Bob

    Friday, August 22, 2014 6:18 PM