Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Look up enum val from combobox editor key/val list

 
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support
View previous topic :: View next topic  
Author Message
jmaeding



Joined: 11 Apr 2008
Posts: 67

PostPosted: Tue Mar 01, 2011 1:14 am    Post subject: Look up enum val from combobox editor key/val list Reply with quote

I have a combobox editor assigned in getcelldata, and am setting its datasource to a key-value list of enums and their descriptions using a helper function called EnumToIList:

comboBox.DataSource = typeof HAHelper.clsImageModStatus).EnumToIList();
comboBox.DisplayMember = "Value";
comboBox.ValueMember = "Key";

since i am doing this programatically, I am using SetValueForCell like this:
Code:
protected override bool SetValueForCell(Row row, Column column, object oldValue, object newValue) {
         // use the row binding to set the value
         //
         RowBinding binding = this.GetBindingForRow(row);
         if (column == vtColStatus) {
            string strVal = (string)newValue;
            List<string> descs = typeof(HAHelper.clsImageModStatus).EnumToDescList();
            int index = 0;
            foreach (string desc in descs) {
               if (desc == strVal) {
                  return binding.SetCellValue(row, column, oldValue, (HAHelper.clsImageModStatus)index);
               }
               index++;
            }
            return false;
         }
         else{
            return binding.SetCellValue(row, column, oldValue, newValue);
         }
      }


This is junky, to look up an enum val by looping through its list of descriptions. The right way is to get the value from the combobox datasource, using the newvalue as key.

How can i access the key-val list? I am wondering if its safe to do, since editor controls are cached and all.
thx
_________________
James M
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Wed Mar 02, 2011 11:39 am    Post subject: Reply with quote

If you are wanting to display user friendly text for enumeration values in a combo box then you should take a look at my article on Code Project

http://www.codeproject.com/KB/cs/LocalizingEnums.aspx

The ResourceEnumConverter class discussed in the article is included in the Infralution.Common assembly. If you use this approach you can set the data source of a combo box directly to the list of enum values and it will display the user friendly text from the resource file. Even easier you can use the UniversalEditBox included in Infralution.Controls and just set the ValueType to your enum type. It will do the rest.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group