SimpleGuy
SimpleGuy

Reputation: 2894

How to bind Enum to ComboBox in a DataGrid

I found this on Stack Overflow, but no where I could get an answer.

I want to bind a ComboBox of a data grid to a property of a class which returns [one of the ] Enum Value.

MyEnum
{
    [StringValue("SomeVal")]
    SomeVal,
    [StringValue("AnotherVal")]
    AnotherVal,
    [StringValue("OneMoreVal")]
    OneMoreVal
}

class MyClass
{
    public MyEnum A_Value
    {
        return whatever; // whatever is MyEnum type
    }
}

Now I made a Data grid column having combo box and there I need to bind a property

myCombo.DataSource = Enum.GetValues(typeof(MyEnum)); 
myCombo.DataBindings.Add("SelectedValue", myDataSource, bindingPath + ".A_Value");

When I run this code, it fails with error

"Cannot set a value to a combo box without a ValueMember"

Then I add below line

myCombo.ValueMember = "Value";

It does not fail this time, but no selected value is set. Can someone help me out with the problem ?


What I referred:

Upvotes: 0

Views: 1961

Answers (2)

Fabio
Fabio

Reputation: 32445

As I understand your datagridview is binded to list of MyClass.
If you using DataGridViewComboBoxColumn type, then use property DatPropertyName.

'Use property name of your class to where combobox will be binding
myCombo.DataPropertyName = "A_Value" 

Then after myCombo.DataSource = Enum.GetValues(typeof(MyEnum));
if value of instanceOf MyClass.A_Value will be in the DataSource of combobox it be shown.

Update If you using normal ComboBox control, then you need specify ValueMember property

myCombo.ValueMember = "A_Value";

Check name of the property is right. Because in you question it is wrong.

Upvotes: 0

DrewJordan
DrewJordan

Reputation: 5314

I'm assuming that myDataSource is supposed to be an implementation of MyClass... Here's an example of how to databind it. It's a little verbose but maybe someone can improve upon it.

public partial class Form2 : Form
{
    private MyClass one;
    private Label label1;
    private ComboBox comboBox1;
    private FlowLayoutPanel panel;
    private Button btn1;
    public Form2()
    {
        InitializeComponent();

        one = new MyClass();
        panel = new FlowLayoutPanel();
        label1 = new Label();
        comboBox1 = new ComboBox();
        btn1 = new Button();
        btn1.Text = "Click to change Property";
        btn1.Click += (sender, args) => { one.A_Value = MyEnum.BtnVal; }; // to test binding to the property

        panel.Dock = DockStyle.Fill;

        Controls.Add(panel);
        panel.Controls.Add(comboBox1);
        panel.Controls.Add(label1);
        panel.Controls.Add(btn1);

        comboBox1.SelectedIndexChanged += (sender, args) =>
        {
            one.A_Value = (MyEnum)(sender as ComboBox).SelectedItem; // update the object when the ComboBox is changed
        };

        comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
        comboBox1.DataBindings.Add("SelectedItem",one,"A_Value"); // update the ComboBox if the Property is changed by something else
        label1.DataBindings.Add("Text",one,"A_Value"); // to show that changes happen to the property and not just the ComboBox
    }



}

public enum MyEnum
{
    [Description("SomeVal")]
    SomeVal,
    [Description("AnotherVal")]
    AnotherVal,
    [Description("OneMoreVal")]
    OneMoreVal,
    [Description("ButtonClickedValue")]
    BtnVal
}

public class MyClass : INotifyPropertyChanged
{
    private MyEnum whatever;

    public MyEnum A_Value
    {
        get { return whatever; } 
        set { whatever = value;
            PropertyChanged(this, new PropertyChangedEventArgs("A_Value"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

Upvotes: 1

Related Questions