Diego Garcia Vieira
Diego Garcia Vieira

Reputation: 93

Binded ComboBox with DataSource

I'm trying to populate the datasource from my combobox with following code:

Here is the part where I create my binding

        _columnsLayoutBinding.DataSource = _myColumnsLayout;
        ColumnsLayoutDataGrid.DataSource = _columnsLayoutBinding;
        DataGridCreator.CurrentInstance.CreateDataGrid<ConfigColumns>(ref ColumnsLayoutDataGrid);

Then inside the DataGridCreator method call, I do some checking and populate the combobox with the values

        var list = (from object type in System.Enum.GetValues(typeof (FontSizeType)) select new KeyValuePair<string, int>(type.ToString(), (int)type)).ToList();
        comboBox.DataSource = list;
        comboBox.DisplayMember = "Key";
        comboBox.ValueMember = "Value";
        return comboBox;

The problem is: When is when a replace the code where I populate the datasource with this:

            var list = new List<KeyValuePair<string, int>>();

            var id = 0;

            foreach (var type in System.Enum.GetValues(typeof(FontSizeType)))
            {
                list.Add(new KeyValuePair<string, int>(type.ToString(), id));
                id++;
            }

            comboBox.DataSource = list;
            comboBox.DisplayMember = "Key";
            comboBox.ValueMember = "Value";

            return comboBox;

It works. The problem seems the value member initial value 0. Then it starts with 0 - ok. When it starts with the enum starting int(6) - Not working.

Here is my enumerator:

[DataContract]
[Serializable]
[Flags]
public enum FontSizeType
{
    [EnumMember]
    Seis = 6,

    [EnumMember]
    Sete = 7,

    [EnumMember]
    Oito = 8,

    [EnumMember]
    Nove = 9,

    [EnumMember]
    Dez = 10,

    [EnumMember]
    Onze = 11

Upvotes: 0

Views: 419

Answers (2)

Algamest
Algamest

Reputation: 1529

Use:

int id = 6;

This would solve your problem. Note: var for this seems unecessary.

For your enum you use the definition [Flags] yet you do not set your enum values to behave in this way. I would recommend removing this or changing you enum values. However considering this specific enum where the value represents the enum element as a number, the values must remain the same.

Here's a link which explains the correct use of Flags: What does the [Flags] Enum Attribute mean in C#?

Upvotes: 0

eoldre
eoldre

Reputation: 1087

In your first example you are populating the KeyValuePair.Values with the integer value of each corresponding Enum. While in the second part you are using a counter starting at zero. Try the below:

        foreach (var type in System.Enum.GetValues(typeof(FontStyle)))
        {
            list.Add(new KeyValuePair<string, int>(type.ToString(), (int)type));
            id++;
        }

Additionally in the first example you are using the FontSizeType Enum and in the 2nd you are using FontStyle. Is that intentional? Not enough context to tell for sure.

Upvotes: 1

Related Questions