Reputation: 3515
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 };
Upvotes: 0
Views: 105
Reputation: 17600
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
Upvotes: 1
Reputation: 3910
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.. :)
Upvotes: 2