Adding enum to combobox

前端 未结 2 1399
说谎
说谎 2021-02-20 11:48

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 Cou         


        
2条回答
  •  感动是毒
    2021-02-20 12:41

    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.

提交回复
热议问题