Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I've created a custom winForm comboBox that allows the user to add items by assigning the Text property of the comboBox then adding it to the ItemsCollection...I'm hosting this custom comboBox in a dataGridView as described in http://msdn.microsoft.com/en-us/library/7tas5c80(v=vs.90).aspx[^]

I can't figure out how add items to the hosted comboBox when the user enters data into the dataGridViewCell that host the my custom comboBox.

Can someone please show me what am I missing here and what am I doing incorrectly?

Thanks in advance.
-DA

example:
public class DataGridViewComboBoxEditCell : DataGridViewTextBoxCell //DataGridViewComboBoxCell
    {
        public DataGridViewComboBoxEditCell()
            :base()
        {
            this.Value = "";
        }

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

            ComboBoxEdit cboEdit = this.DataGridView.EditingControl as ComboBoxEdit;
            
            if (this.Value == null)
            {
                cboEdit.Add((string)this.DefaultNewRowValue);
            }
            else
            {
                cboEdit.Add((string)this.Value);
            }
        }


        public override Type EditType
        {
            get{return typeof(ComboBoxEdit);}
        }

        public override Type ValueType
        {
            get{ return typeof(String);}
        }

        public override object DefaultNewRowValue
        {
            get { return string.Empty; }
        }
    }

 public class ComboBoxEdit : ComboBox, IDataGridViewEditingControl
    {
        DataGridView grid;
        private bool valueChanged = false;
        int rowIndex;

        public ComboBoxEdit()
        {
            this.Text = "";
            this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.DropDownStyle = ComboBoxStyle.DropDown;
        }

        public void Insert(string item)
        {
            if (item != null && item.Length > 0)
            {
                this.Items.Insert(0, item);
                this.Text = "";
            }
        }

        public void Add(string item)
        {
            if (item != null && item.Length > 0)
            {
                this.Items.Add(item);
                this.Text = "";
            }
        }

        protected override void OnSelectedValueChanged(EventArgs e)
        {
            this.Items.RemoveAt(this.SelectedIndex);
            this.Text = "";
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && this.Text.Length > 0)
            {
                Add(this.Text.ToUpper());
            }
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (e.Index < 0)
                return;

            string txt = (string)base.Items[e.Index];

            e.DrawBackground();

            Icon icon = Properties.Resources.delete;

            e.Graphics.DrawImage(icon.ToBitmap(), e.Bounds.X, e.Bounds.Y, 15, 15);

            e.Graphics.DrawString(txt, base.Font, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));

            e.DrawFocusRectangle();
        }
 

        // Implmentation of IDataGridViewEditingControl Interface

        // Implementation of properites
        public DataGridView EditingControlDataGridView
        {
            get { return grid; }
            set { grid = value; }
        }

        public object EditingControlFormattedValue
        {
            get{return this.Text;}
            set
            {
                this.Text = (string)value;
                Add((string)value);
            }           
        }

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

        public bool EditingControlValueChanged
        {
            get { return valueChanged; }
            set { valueChanged = value; }
        }

        public Cursor EditingPanelCursor
        {
            get { return base.Cursor; }
        }

        public bool RepositionEditingControlOnValueChange
        {
            get { return false; }
        }


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

        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {
            //switch (key & 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:
            //        return true;
            //    default:
            //        return !dataGridViewWantsInputKey;
            //}
            return false;
        }

        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {
        }
    }

 public enum CtrlTypes
    {
        Chk,
        Cbo,
        Txt,
        Btn
    }

    public partial class Form1 : Form
    {
        List<CtrlTypes> ct;
        ComboBoxEdit cboEdit;

        public Form1()
        {
            InitializeComponent();

            grid.AllowUserToAddRows = false;
            grid.RowHeadersVisible = false;

            ct = new List<CtrlTypes>();

            int cnt = 0;
            foreach (CtrlTypes cts in Enum.GetValues(typeof(CtrlTypes)))
            {
                DataGridViewRow row = null;
                DataGridViewCell celltype = null;
                DataGridViewTextBoxCell cellname = null;

                switch (cts)
                {
                    case CtrlTypes.Btn:
                        row = new DataGridViewRow();
                        celltype = new DataGridViewButtonCell();
                        celltype.Value = "Btn";
                        cellname = new DataGridViewTextBoxCell();
                        cellname.Value = "Button:";
                        row.Cells.AddRange(new DataGridViewCell[] { cellname, celltype });
                        break;
                    case CtrlTypes.Cbo:
                        row = new DataGridViewRow();
                        celltype = new DataGridViewComboBoxEditCell();
                        //celltype.Value = "GOOG";
                        cellname = new DataGridViewTextBoxCell();
                        cellname.Value = "ComboBx:";
                        row.Cells.AddRange(new DataGridViewCell[] { cellname, celltype });
                        break;
                    case CtrlTypes.Chk:
                        row = new DataGridViewRow();
                        celltype = new DataGridViewCheckBoxCell();
                        celltype.Value = true;
                        cellname = new DataGridViewTextBoxCell();
                        cellname.Value = "ChkBx:";
                        row.Cells.AddRange(new DataGridViewCell[] { cellname, celltype });
                        break;
                    case CtrlTypes.Txt:
                        row = new DataGridViewRow();
                        celltype = new DataGridViewTextBoxCell();
                        celltype.Value = "Hello World";
                        cellname = new DataGridViewTextBoxCell();
                        cellname.Value = "TxtBx:";
                        row.Cells.AddRange(new DataGridViewCell[] { cellname, celltype });
                        break;
                }
                grid.Rows.Add(row);
            }
        }
    }
Posted

1 solution

Can you try out the Other Way like using ItemTemplate property of DataGridView,
Place the Combo/DropDownBox in one column and place the Checkbox control in the GridView cell in 1stcolumn of Datagridview , when user clicks on the checkbox, then enable the textbox control in the GridCell, if user enter Data in the textbox then Add that to the ListItems of ddl placed in another column
 
Share this answer
 
Comments
d.allen101 29-Oct-13 9:42am    
thanks for the reply but i'm not understanding what your advising me to try. can you please give an example? also I need to be able to host different controls in the same column. the controls are added at runtime dynamically based on a message received from a external dataFeed. I want my comboBox to be able receive user input and add it to the dropDownList for display.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900