0
votes

I have a form with a combobox control. In the form load event handler I set the datasource property of the combobox to the values of an enumeration.

public class myEnum
    {
       public static myEnum DOCUMENTO =
           new myEnum("0", "DOCUMENTO");
       public static myEnum BLOCCATO =
           new myEnum("1", "BLOCCATO");
       public static myEnum NONBLOCCATO =
           new myEnum("2", "NONBLOCCATO");
       static myEnum() {}

       public myEnum (string code, 
                                   string descCaptionCode)
         : base(code, descCaptionCode){}

       public myEnum() {}

    }


private void loadLockStateType()
        {
            List<myEnum> values = new List<myEnum>();
            values.Add(myEnum.DOCUMENTO);
            values.Add(myEnum.BLOCCATO);
            values.Add(myEnum.NONBLOCCATO);

            myCombo.DataSource = values;
        }

This works fine and I fill out my form and click save. My problems is when I want to reload this form with the saved data. I can put all the info back except I'm not sure how to set the combobox from the enumeration value in my fetched data.

i've tried with this

myCombo.SelectedItem = elemento.ReadOnly != null ? elemento.ReadOnly : LockStateObjectEnum.DOCUMENTO.Code;

where elemento is a class that have a String field that would represent one of the 3 possible value in the comboBox

public Class Elemento 
{
 ...
 public string ReadOnly
        {
            get;
            set;
        }
 ...
} 

but it doesn't work. It read well the value in elemento, for example elemento.ReadOnly = "2", but the combo always selected the first element myCombo.SelectedItem={0} . I've Also tried with myCombo.SelectedValue, but it returns in a exception stating: InvalidOperationException. I think the problem is that the combo is loaded with an enum and i've tried to set a value with a string, but i can't put the enum also in Elemento Class. How can i do?

Thanks

1

1 Answers

1
votes

try Text instead of SelectedItem

myCombo.Text = elemento.ReadOnly != null ? elemento.ReadOnly : LockStateObjectEnum.DOCUMENTO.Code;