0

I have combobox cmbBoxA which is populated like this

private void FillComboBox() { foreach (var a in Helpers.Helper.GetData()) { cmbBoxA.Items.Add(a); } } 

GetData has this code

public static List<VATMode> GetData() { List<VATMode> vatModes = Enum.GetValues(typeof(VATMode)). Cast<VATMode>().ToList(); return vatModes; } 

Now I'm trying to fetch selected combobox item is with

int sel = Convert.ToInt16(cmbBoxA.SelectedValue); 

but It's always 0?

update: enum is

public enum VATMode { A = 1, B = 2, C = 3 }; 
0

2 Answers 2

2

The Enum

public enum Status { Active = 0, Canceled = 3 }; 

Setting the drop down values from it

cbStatus.DataSource = Enum.GetValues(typeof(Status)); 

Getting the enum from the selected item

Status status; Enum.TryParse<Status>(cbStatus.SelectedValue.ToString(), out status); 

I hope it will help you.. :)

Sign up to request clarification or add additional context in comments.

Comments

1

ComboBox.SelectedValue is used when your ComboBox is databound and yours is not.

Change your code like this:

private void FillComboBox() { cmbBoxA.DataSource = Helpers.Helper.GetData(); } 

and to get value back:

VATMode value; Enum.TryParse<VATMode>(cbStatus.SelectedValue.ToString(), out value); 

and then you can cast it to int

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.