Windows Develop Bookmark and Share   
 index > Windows Forms General > How to create a custom Datagridview ComboBox Column
 

How to create a custom Datagridview ComboBox Column

I have tried to create a custom datagridviewcombobox column, but so far i have not been able to make it work because when clicking on the comboboxcolumn i get the following error "Object Reference not set to an instance of an object".

I have search but not been able to find the example that i need, i found a sample that switches between a textbox and a combobox, but then i tried severeal times to modified it so it will always be a combobox column and have not been able to do it. Can someone provide give a samplel code please

What i have is this

    public class ComboBoxColumn : DataGridViewComboBoxColumn
    {       
        public ComboBoxColumn()
        {
            ComboBoxCell cbc = new ComboBoxCell();
            this.CellTemplate = cbc;
        }

    }

    public class ComboBoxCell : DataGridViewComboBoxCell
    {
        public ComboBoxCell(): base()
        {
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            ComboBoxControl ctl = DataGridView.EditingControl as ComboBoxControl;
            ctl.DropDownStyle = ComboBoxStyle.DropDownList;
           
            if (ctl != null)
            {
                ctl.SelectedIndex = 0;
            }
        }
    }


    public class ComboBoxControl :  ComboBox, IDataGridViewEditingControl
    {
        private int index_ = 0;
        private DataGridView dataGridView_ = null;
        private bool valueChanged_ = false;

        public ComboBoxControl() : base()
        {
            this.SelectedIndexChanged += new EventHandler(ComboBoxControl_SelectedIndexChanged);
        }


        public void ComboBoxControl_SelectedIndexChanged(object sender, EventArgs e)
        {
  //I will add code here
            NotifyDataGridViewOfValueChange();
        }


        protected virtual void NotifyDataGridViewOfValueChange()
        {
            this.valueChanged_ = true;
            if (this.dataGridView_ != null)
            {
                this.dataGridView_.NotifyCurrentCellDirty(true);
            }
        }

        #region IDataGridViewEditingControl members

       
        public  void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
            this.BackColor = dataGridViewCellStyle.BackColor;
        }
       

        public  DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView_;
            }
            set
            {
                dataGridView_ = value;
            }
        }

        public  object EditingControlFormattedValue
        {
            get
            {
                return base.SelectedValue;
            }
            set
            {
                base.SelectedValue = value;
                NotifyDataGridViewOfValueChange();
            }
        }

        public  int EditingControlRowIndex
        {
            get
            {
                return index_;
            }
            set
            {
                index_ = value;
            }
        }

        public  bool EditingControlValueChanged
        {
            get
            {
                return valueChanged_;
            }
            set
            {
                valueChanged_ = value;
            }
        }
       
        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            if (keyData == Keys.Return)
                return true;
            switch (keyData & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                case Keys.Return:
                    return true;
                default:
                    return false;
            }
        }
              
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }
                 
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue.ToString();
        }
              
        public void PrepareEditingControlForEdit(bool selectAll)
        {
        }

        public bool RepositionEditingControlOnValueChange
        {
            get { return false; }
        }
    }

 

  • Moved byHarry ZhuMSFTMonday, October 05, 2009 5:11 AMrelating to datagridview (From:Visual C# General)
  •  
gustavo_e_oviedo  Friday, October 02, 2009 2:00 AM

Hi,

 

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)

        {

            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

 

            ComboBoxControl ctl = DataGridView.EditingControl as ComboBoxControl;

 

            //comment out this line

            //ctl.DropDownStyle = ComboBoxStyle.DropDownList;

 

            if (ctl != null)

            {

                ctl.SelectedIndex = 0;

            }

        }

 

If you want to change the style, you can set it in combboxcontrol constructor.

        public ComboBoxControl()

            : base()

        {

            this.DropDownStyle = ComboBoxStyle.DropDownList;

            this.SelectedIndexChanged += new EventHandler(ComboBoxControl_SelectedIndexChanged);

        }

 

DataGridViewCell.InitializeEditingControl Method

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.initializeeditingcontrol.aspx

 

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  16 hours 17 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• cannot convert from 'string' to 'AccountManagement.IAccount'
• Reading txt file to textbox
• C# and controlling window behaviour
• status strip no longer visible after form resize
• Selected Listview value
• Make form run in full sceen
• how to create workspaces of related child forms
• How to minimize all windows
• Printing letters
• Debuging Windows Form Error?!