Hi long,
From your descript, you bound a Enum property to a DataGridViewComboBoxColumn and your issue is the combo box do not show the default value of property and do notset the property from drop list. Please feel free to let me know if I misunderstand you.
Based on my understanding, we can bind a binding list to a DataGridViewComboBoxColumn, but it is not very good to bind a property which refers to an object to a DataGridViewComboBoxColumn. Instead, we can bind this property to a ComboBox. We can show and edit the value of this property via the ComboBox. The code snippet below shows how to bind a property of an enum type to a ComboBox:
private ClusterLetterType _closuretype = ClusterLetterType.ApplicantEP;
public ClusterLetterType CLOSURE_TYPE
{
get { return _closuretype; }
set
{
if (value != _closuretype)
{
_closuretype = value;
//Update data to combobox.
this.comboBox1.SelectedItem = value;
}
}
}
private void BindEnumForm_Load(object sender, EventArgs e)
{
this.comboBox1.DataSource = Enum.GetValues(typeof(ClusterLetterType));
//Add databinding between the SelectedItem property of ComboBox and the CLOSURE_TYPE property of the form.
this.comboBox1.DataBindings.Add("SelectedItem", this, "CLOSURE_TYPE", true, DataSourceUpdateMode.OnPropertyChanged);
}
private void button1_Click(object sender, EventArgs e)
{
this.CLOSURE_TYPE = ClusterLetterType.ApplicantNoEP;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(CLOSURE_TYPE.ToString());
}
In the code snippet, I directly set the SelectedItem of the ComboBox when we set the value of the property. A more formal way to do that is implementing the INotifyPropertyChanged interface in the data source. You can get more information and a sample about from:
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx.
If we want to bind a BindingList to a DataGridViewComboBoxColumn, we need to define an object which contains a property of the enum type. Then we can bind a BindingList with that object type to the DataGridView. This is a code snippet:
private BindingList<BindingObject> _bindingList = null;
public BindingList<BindingObject> BindingList
{
get { return _bindingList; }
set { _bindingList = value; }
}
private void BindEnumForm_Load(object sender, EventArgs e)
{
_bindingList = new BindingList<BindingObject>();
_bindingList.Add(new BindingObject { CLOSURE_TYPE = ClusterLetterType.ApplicantEP });
_bindingList.Add(new BindingObject { CLOSURE_TYPE = ClusterLetterType.ApplicantNoEP });
this.dgvClosures.AutoGenerateColumns = false;
this.dgvClosures.DataSource = _bindingList;
this.dgvClosureBx.DataSource = Enum.GetValues(typeof(ClusterLetterType));
this.dgvClosureBx.DataPropertyName = "CLOSURE_TYPE";
this.dgvClosureBx.HeaderText = "CLOSURE_TYPE";
this.dgvClosureBx.Name = "CLOSURE_TYPE";
dgvClosures.Columns.Add(dgvClosureBx);
}
//Binding object
public class BindingObject
{
//Enum property.
public ClusterLetterType CLOSURE_TYPE { set; get; }
}
This is another sample:
http://msdn.microsoft.com/en-us/library/y0wfd4yz.aspx.
Let me know if this helps.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.