我想在DataGridView列中加入自己定义的一个用户控件,做一些配置工作
我的用户控件主要有两个ComBox,要实现的效果就是COmBox的内容改变后,对应单元格可以获取改变后的内容
自定义控件代码如下
public partial class OperateUserControl : UserControl
    {
        private Content config;
        public OperateUserControl()
        {
            InitializeComponent();
        }
        public Content Config
        {
            get
            {
                return config;
            }
            set
            {
                config = value;
            }
        }        private void OperateUserControl_Load(object sender, EventArgs e)
        {
            config = new Content();
            comboBoxSort.DataBindings.Add("Text", config, "ContentType");
            comboBoxValue.DataBindings.Add("Text", config, "ContentValue");
        }
        protected virtual void OnValueChanged(EventArgs eventargs)
        {        }    }
    public class Content : Object
    {
        string contentType;        public string ContentType
        {
            get { return contentType; }
            set { contentType = value; }
        }
        string contentValue;        public string ContentValue
        {
            get { return contentValue; }
            set { contentValue = value; }
        }
    }实现自定义列的代码如下
    public class DataGridViewConfigColumn : DataGridViewColumn
    {
        public DataGridViewConfigColumn()
            : base(new DataGridViewConfigCell())
        {
        }
        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a CalendarCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(DataGridViewConfigCell)))
                {
                    throw new InvalidCastException("Must be a ConfigCell");
                }
                base.CellTemplate = value;
            }
        }
    }    public class DataGridViewConfigCell : DataGridViewCell
    {
        public DataGridViewConfigCell()
            : base()
        {
           
        }
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
        }
        public override Type EditType
        {
            get
            {
                return typeof(DataGridViewConfigControl);
            }
        }
        public override Type ValueType
        {
            get
            {
                return typeof(Content);
            }
        }
        public override object DefaultNewRowValue
        {
            get
            {
                return base.DefaultNewRowValue;
            }
        }
    }    class DataGridViewConfigControl : OperateUserControl, IDataGridViewEditingControl
    {
         DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;        public DataGridViewConfigControl()
        {
            //this.Format = DateTimePickerFormat.Short;
        }        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue
        // property.
        public object EditingControlFormattedValue
        {
            get
            {
                return this.Config;
            }
            set
            {
                Content newValue = value as Content;
                if (newValue != null)
                {
                    this.Config = newValue;
                }
            }
        }        // Implements the IDataGridViewEditingControl.GetEditingControlFormattedValue
        // method.
        public object GetEditingControlFormattedValue(
            DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }        // Implements the IDataGridViewEditingControl.ApplyCellStyleToEditingControl
        // method.
        public void ApplyCellStyleToEditingControl(
            DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
            this.BackColor = dataGridViewCellStyle.BackColor;
        }        // Implements the IDataGridViewEditingControl.EditingControlRowIndex
        // property.
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
        // method.
        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {
            // Let the DateTimePicker handle the keys in the next list.
            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 false;
            }
        }        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
        // method.
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparations is needed.
        }        // Implements the IDataGridViewEditingControl.RepositionEditingControlOnValueChange
        // property.
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }        // Implements the IDataGridViewEditingControl.EditingControlDataGridView
        // property.
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }        // Implements the IDataGridViewEditingControl.EditingControlValueChanged
        // property.
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }        // Implements the IDataGridViewEditingControl.EditingPanelCursor
        // property.
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }        protected override void OnValueChanged(EventArgs eventargs)
        {
            // Notify the DataGridView when the cell's contents was changed.
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnValueChanged(eventargs);
        }    }可以加入DataGridView列,并且编译成功通过
可是显示的时候列中的内容为空白
大虾们给帮帮忙了

谢谢