Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagridviewcomboboxcolumn bind with bindinglist and enum issues
 

Datagridviewcomboboxcolumn bind with bindinglist and enum issues

I got a binglist with a property, type is enum. I need to show itto Datagridviewcomboboxcolumn and change the property by click from drop list. the combo box do not show the default value of property and do notset the property from drop list. Any ideas?


datagrid part.

this.dgvClosureBx.DataSource=Enum.GetValues(typeof(ClusterLetterType));

this.dgvClosureBx.DataPropertyName = "CLOUSURE_TYPE";

this.dgvClosureBx.ValueType = typeof(ClusterLetterType);

dgvClosures.Columns.Add(dgvClosureBx);




property part

public ClusterLetterType CLOSURE_TYPE

{get { return _closuretype; }

set

{
if
(_allEdit)

_closuretype= value;

}

}

public

enum ClusterLetterType

{

UnDefine,

ApplicantEP,

ApplicantNoEP,

NoApplicationEP,

NoApplicationNoEP,

}

  • Edited bylong cui Friday, August 28, 2009 8:36 AM
  • Edited bylong cui Friday, August 28, 2009 8:36 AM
  • Edited bylong cui Friday, August 28, 2009 8:33 AM
  •  
long cui  Friday, August 28, 2009 8:32 AM

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.
Aland Li  Tuesday, September 01, 2009 8:03 AM

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.
Aland Li  Tuesday, September 01, 2009 8:03 AM
Hi long,

Could you please let me know if my reply helps?

Regards,
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.
Aland Li  Thursday, September 03, 2009 8:47 AM

You can use google to search for other answers

Custom Search

More Threads

• How do I allow the user to enter a negative number in a column on a grid with validation?
• How To update four related tables using a TableAdapter
• metoh with list of parameters
• CellFormatting event slow datagridview
• Form Combobox updating a datagridview column
• When to use Databinding in Windows Form
• The next editing cell is the one in the same row, not the one in the column below.
• How to force the sorting glyph to show?
• How to add the checkbox in a DataGrid using Windows Forms [ 2.0 Framework]?
• How to improve performance of datagridview