如何在dataGridView中向托管的customComboBox添加项目 [英] How to add items to hosted customComboBox in dataGridView

查看:58
本文介绍了如何在dataGridView中向托管的customComboBox添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个自定义的winForm组合框,允许用户通过分配comboBox的Text属性然后将其添加到ItemsCollection来添加项目......我在dataGridView中托管这个自定义组合框,如 http://msdn.microsoft.com/en-us/library/7tas5c80(ⅴ = vs.90).aspx [ ^ ]



当用户将数据输入dataGridViewCell时,我无法弄清楚如何向托管的comboBox添加项目托管我的自定义组合框。



有人可以告诉我这里我错过了什么,我做错了什么?



谢谢提前。

-DA



例如:

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);
            }
        }
    }

推荐答案

你能尝试其他方式,比如使用ItemTemplate属性DataGridView,

将Combo / DropDownBox放在一列中,并将Checkbox控件放在Datagridview的1st列中的GridView单元格中,当用户单击复选框时,然后在GridCell中启用文本框控件,如果用户在文本框中输入数据然后将其添加到放置在另一列中的ddl的ListItems
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


这篇关于如何在dataGridView中向托管的customComboBox添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆