Bind combobox selectedvalue using enumeration

I was unable to bind the selected combobox value.

 public void InitializePage()
 {          
    cbStatus.DataSource = Enum.GetValues(typeof(CourseStudentStatus));
 }

in my constructor

 public EditCourseForm(int status)
 {
     InitializePage();                      
     cbStatus.SelectedText = Enum.GetName(
        typeof(CourseStudentStatus), status).ToString();         
 }

I also tried this. cbStatus.SelectedValue = Status

but I cannot set SelectedValue to ComboBox.

Update My listing

 public enum CourseStudentStatus
{
    Active = 1,
    Completed = 2,
    TempStopped = 3,
    Stopped = 4,
}
+3
source share
3 answers

The problem is resolved.
cbStatus.SelectedItem = (CourseStudentStatus)status;

Hope this helps.

+7
source

You tried

public EditCourseForm(CourseStudentStatus status)
{
    InitializePage();            

    cbStatus.SelectedItem= status;
}
+1
source

Change the code of the InitializePage () function to

public void InitializePage () {

    cbStatus.DataTextField = Enum.GetName(typeof(CourseStudentStatus));

    cbStatus.DataValueField = Enum.GetValues(typeof(CourseStudentStatus));
}

Update Try this.

var itemValues = Enum.GetValues(typeof(CourseStudentStatus)).Cast<CourseStudentStatus>().ToDictionary(obj => obj.ToString(), obj => obj.GetHashCode()).ToList();
        comboBox1.DisplayMember = "Key";
        comboBox1.ValueMember = "Value";
        comboBox1.DataSource = itemValues;

here itemValues ​​is a type List<KeyValuePair<string, int>>

0
source

All Articles