Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Databindings of combobox and enum in c#
 

Databindings of combobox and enum in c#

Hello,
I created bindingSource from a DataObject of my class let's call it classA, which as a property of an enum type, let's call it enumA, now I want to databind that property to a combo box, so when user selects a different item in combo box it should automatically update my instance of classA, and here is the code:

this.comboBox1.DataBindings.Add( new System.Windows.Forms.Binding( "SelectedValue", this.bindingSource1, "GroupItems", true ) );

however the combo box is empty, I thought of setting the combobox in a different way like :

comboBox1.DataSource = Enum.GetValues( typeof( LocalSettings.GroupItemsBy ) );

and add code to SelectedIndexChanged to set the properties value based on combo box, but then it won't be using the datasource and I have to sync them myself.

Does anyone know how to populate combo box so that it would update its datasource.

Many Thanks,
JefeSol  Monday, September 14, 2009 4:31 PM
Hi JefeSol,

Based on my understanding, you want to use Enum type as the DataSource of ComboBox. But enum type doesn't offer that binding support. Enum.GetValues(typeof(LocalSettings.GroupItemsBy)); will return an Array type object, you are binding comboBox to that array type not the enum type. When doing this, if you retrieve the SelectedValue of ComboBox1, you will see the Text of the selected item.

So I suggest you to create a custom object first and make a list of it. Here is an example
public class MyType
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}

private string name;
public string Name
{
get { return name; }
set { name = value; }
}

public MyType(int id, string name)
{
this.id = id;
this.name = name;
}
}

You can use it as below
List<MyType> myList = new List<MyType>();
myList.Add(new MyType(1, "Item1"));
myList.Add(new MyType(2, "Item2"));
comboBox1.DataSource = myList;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";

If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Wednesday, September 16, 2009 7:49 AM
Hi JefeSol,

Based on my understanding, you want to use Enum type as the DataSource of ComboBox. But enum type doesn't offer that binding support. Enum.GetValues(typeof(LocalSettings.GroupItemsBy)); will return an Array type object, you are binding comboBox to that array type not the enum type. When doing this, if you retrieve the SelectedValue of ComboBox1, you will see the Text of the selected item.

So I suggest you to create a custom object first and make a list of it. Here is an example
public class MyType
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}

private string name;
public string Name
{
get { return name; }
set { name = value; }
}

public MyType(int id, string name)
{
this.id = id;
this.name = name;
}
}

You can use it as below
List<MyType> myList = new List<MyType>();
myList.Add(new MyType(1, "Item1"));
myList.Add(new MyType(2, "Item2"));
comboBox1.DataSource = myList;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";

If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Wednesday, September 16, 2009 7:49 AM
Thanks Kira for your reply, your answer kind of solve my problems, I think the original problem is not right, basically I wanted to bind a combo box with an object's attribute which is an enum and by changing the combo box and selecting different items it would automatically update the object's attribute. I think it's not possible to do it automatically so I ended up adding code to combobox handler to sync it with object's attribute, but your answer helped me to understand this better. so thanks.
JefeSol  Monday, September 21, 2009 3:56 PM

You can use google to search for other answers

Custom Search

More Threads

• override a treenode class in c#
• Differing number of rows in datagridview and the dataset it is bound to
• how to set dataType of dataTable column as byte[]
• object databinding - combobox lookup
• [OleDB, DataSet] How to insert new data into database
• custom dgv, binding, repeating columns
• DatGridViewColumn.DisplayIndex property not setting the requested value.
• Scrollbar on DataGridView stops working after data refresh
• Appending in datatable
• DataGridView grid color? divider height?