Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
this days,i have written a customize datagirdviewcolumn like the DataGridViewCalendarColumn in MSDN.but there are many problem in my code.the main problem is after edit this cell's control ,control's value can't display in the cell.
the following is my code:
edit control code:
C#
public partial class ItemSelectForDgv : UserControl
    {
        public ItemSelectForDgv()
        {
            InitializeComponent();
        }
        private String _ItemNum;
        [DefaultValue("")]
        public String ItemNum
        {
            set { _ItemNum = value; }
            get {
                if (String.IsNullOrEmpty(_ItemNum)) return String.Empty;
                return _ItemNum; }
        }

        private void btnShowItemSelectForm_Click(object sender, EventArgs e)
        {
            frmControlGDItemSelect frm = new frmControlGDItemSelect();
            frm.ShowDialog();
        }

        private void txbItemNum_TextChanged(object sender, EventArgs e)
        {
            changeValue();
        }
        public void changeValue()
        {

            this.ItemNum = txbItemNum.Text.ToString().Trim();
        }
    }

Customize DataGridViewColumn code:
C#
public class DataGridViewItemColumn : DataGridViewColumn
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public DataGridViewItemColumn()
            : base(new DataGridViewItemCell())
        {
        }
        /// <summary>
        /// 获取或设置用于创建新单元格的模板。
        /// </summary>
        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(DataGridViewItemCell)))
                {
                    throw new InvalidCastException("Must be a DgvItemSelect");
                }
                base.CellTemplate = value;
            }
        }
    }
    /// <summary>
    /// 日期选择DataGridViewTextBoxCell
    /// </summary>
    public class DataGridViewItemCell : DataGridViewTextBoxCell
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public DataGridViewItemCell()
            : base()
        {
        }
        /// <summary>
        /// 附加并初始化寄宿的编辑控件。
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="initialFormattedValue"></param>
        /// <param name="dataGridViewCellStyle"></param>
        public override void InitializeEditingControl(int rowIndex, object
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
                dataGridViewCellStyle);
            DataGridViewItemSelectEditingControl ctl =
                DataGridView.EditingControl as DataGridViewItemSelectEditingControl;
            try
            {
                //base.Value = (String)this.Value;
                ctl.ItemNum = (String)this.Value;
                //this.ErrorText = this.Value.ToString();
            }
            catch
            {
                ctl.ItemNum = "";
            }
        }
        
        /// <summary>
        /// 编辑时的类型
        /// </summary>
        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that CalendarCell uses.
                return typeof(DataGridViewItemSelectEditingControl);
            }
        }
        ///// <summary>
        ///// 值类型
        ///// </summary>
        public override Type ValueType
        {
            get
            {
                // Return the type of the value that CalendarCell contains.
                return typeof(String);
            }
        }
        ///// <summary>
        ///// 默认值
        ///// </summary>
        public override object DefaultNewRowValue
        {
            get
            {
                // Use the current date and time as the default value.
                return this.Value;
            }
        }
    }
    /// <summary>
    /// DateTimePicker单元格控件
    /// </summary>
    public class DataGridViewItemSelectEditingControl : ItemSelectForDgv, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;
        /// <summary>
        /// 初始化
        /// </summary>
        public DataGridViewItemSelectEditingControl()
        {
            //this.Format = DateTimePickerFormat.Short;            
        }

        /// <summary>
        /// 获取或设置编辑器正在修改的单元格的格式化值。
        /// </summary>
        public object EditingControlFormattedValue
        {
            get
            {
                return this.ItemNum.ToString();
            }
            set
            {
                if (value is String)
                {
                    this.ItemNum = (String)value;
                }
            }
        }
        /// <summary>
        /// 检索单元格的格式化值。
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public object GetEditingControlFormattedValue(
            DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }
        /// <summary>
        /// 更改控件的用户界面 (UI),使之与指定单元格样式一致。
        /// </summary>
        /// <param name="dataGridViewCellStyle"></param>
        public void ApplyCellStyleToEditingControl(
            DataGridViewCellStyle dataGridViewCellStyle)
        {
        }
        /// <summary>
        /// 获取或设置该承载单元格的父行的索引。
        /// </summary>
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }
        /// <summary>
        /// 确定指定的键是应由编辑控件处理的常规输入键,还是应由 System.Windows.Forms.DataGridView 处理的特殊键。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dataGridViewWantsInputKey"></param>
        /// <returns></returns>
        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {
            // Let the DateTimePicker handle the keys listed.
            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;
            }
        }
        /// <summary>
        /// 准备当前选中的单元格以进行编辑。
        /// </summary>
        /// <param name="selectAll"></param>
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.
        }
        /// <summary>
        /// 获取或设置一个值,该值指示每当值更改时,是否需要重新定位单元格的内容。
        /// </summary>
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return true;
            }
        }
        /// <summary>
        /// 获取或设置包含单元格的 System.Windows.Forms.DataGridView。
        /// </summary>
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }
        /// <summary>
        /// 获取或设置一个值,该值指示编辑控件的值是否与承载单元格的值不同。
        /// </summary>
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }
        /// <summary>
        /// 获取当鼠标指针位于 System.Windows.Forms.DataGridView.EditingPanel 上方但不位于编辑控件上方时所使用的光标。
        /// </summary>
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }
        /// <summary>
        /// 重写数据变化方法
        /// </summary>
        /// <param name="eventargs"></param>
        protected override void OnTextChanged(EventArgs eventargs)
        {
            // Notify the DataGridView that the contents of the cell
            // have changed.
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnTextChanged(eventargs);
            
        }
       
    }
Posted

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