10

Hi may i know how to get the enum value below to bind into the combobox? I wrote the below code which works well but wonder is this the best way.

public enum CourseStudentStatus
{
  Active = 1,
  Completed = 2,
  TempStopped = 3,
  Stopped = 4,
}

//Bind Course Status
Dictionary<string, int> list = new Dictionary<string, int>();
foreach (int enumValue in Enum.GetValues(typeof(CourseStudentStatus)))
  list.Add(Enum.GetName(typeof(CourseStudentStatus), enumValue), enumValue);
var column = ((DataGridViewComboBoxColumn)dgv.Columns["studentCourseStatus"]);
column.DataPropertyName = "StudentStatus";              
column.DisplayMember = "Key";
column.ValueMember = "Value";
column.DataSource= list.ToList();

----------------- UPDATE -------------------
Hi i have changed my code to this according to Sanjeevakumar Hiremat and it works perfectly.

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

However, when i want to a Get() and want to bind the value back to the cbStatus, it cast error {"Object reference not set to an instance of an object."}
cbStatus.SelectedValue = Course.Status;.

The cbStatus.Datasource is not empty and it has value after bound to cbStatus.DataSource = Enum.GetValues(typeof(CourseStudentStatus));

please advice.

VeecoTech
  • 2,011
  • 7
  • 36
  • 47
  • i am thinking is it possible to reduce this junk of code? or could this be converted to a function? – VeecoTech Mar 18 '11 at 10:35
  • you could put your conversion to a dictionary into an extension method for System.Enum, which would make your code more readable and general, but I don't think there is a way to bind to an enum without doing all the GetValues and GetName stuff. – Paolo Falabella Mar 18 '11 at 10:52

2 Answers2

18

Following should be the simplest way to bind it.

column.DataSource = Enum.GetValues(typeof(CourseStudentStatus));

To get the selected value you need to cast it to the enum type.

CourseStudentStatus selectedValue = (CourseStudentStatus)column.SelectedValue

Enum.GetValues returns an array of the enumType values which can then be bound to any control.

I've tested this on a standalone combobox, not in a combobox column in DataGridView, YMMV.

Sanjeevakumar Hiremath
  • 10,299
  • 3
  • 38
  • 46
4

I don't think there is a best way. I used to do something similar with a GenericListItem<T> class where T is the backing value, in your case, an enum.

This class exposed Display string and Value T properties to bind to. I think I was also overriding ToString because it is the default if you don't specify the DisplayMember. I went further and made a constructor taking just Value and defaulting Display to Value.ToString, which in the case of enums works I think.

I'd then make a List<GenericListItem<T>>, feed that into the DataSource of the column and set the DisplayMember and ValueMember properties accordingly in code. This list is the alternative to the dictionary used in your example.

But I'm not saying it's a better solution :-) however it means you can remove code, say enum iteration, into this class or specialise the class for handling certain data types better, all with the end goal of being inserted into a list and bound to a control.

Adam Houldsworth
  • 60,104
  • 9
  • 137
  • 177
  • Can you tell what you set the DisplayMember and ValueMember to? – Kirsten Greed Jan 11 '14 at 22:51
  • 1
    @kirsteng The `Display` property was the `DisplayMember` and the `Value` property was the `ValueMember`. My class was generic so you also had to provide a way to get the string representation of whatever the value was. You could do that using a `Func` or something. – Adam Houldsworth Jan 12 '14 at 08:55