Exemple #1
0
        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);
            DataGridViewDateTimePickerEditingControl ctl =
                DataGridView.EditingControl as DataGridViewDateTimePickerEditingControl;

            try
            {
                ctl.Value = (DateTime)this.Value;
            }
            catch (ArgumentOutOfRangeException oorEx)
            {
                ctl.Value = System.DateTime.Now;
            }
            catch (NullReferenceException nullEx)
            {
                ctl.Value = System.DateTime.Now;
            }

            DataGridViewDateTimePickerColumn owningCol = this.OwningColumn as DataGridViewDateTimePickerColumn;

            if (owningCol != null)
            {
                ctl.Format       = owningCol.Format;
                ctl.CustomFormat = owningCol.CustomFormat;
                ctl.ShowUpDown   = owningCol.ShowUpDown;
            }
        }
Exemple #2
0
        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            // 设置显示的时间和日期格式
            if (this.RowIndex < 0 || this.DataGridView.Rows[this.RowIndex].IsNewRow)
            {
                return(string.Empty);
            }

            DataGridViewDateTimePickerColumn owningCol = this.OwningColumn as DataGridViewDateTimePickerColumn;

            if (owningCol != null)
            {
                switch (owningCol.Format)
                {
                case DateTimePickerFormat.Custom:
                    this.Style.Format = owningCol.CustomFormat;
                    break;

                case DateTimePickerFormat.Long:
                    this.Style.Format = "D";
                    break;

                case DateTimePickerFormat.Short:
                    this.Style.Format = "d";
                    break;

                case DateTimePickerFormat.Time:
                    this.Style.Format = "T";
                    break;
                }
            }

            return(base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context));
        }
Exemple #3
0
        public override object Clone()
        {
            DataGridViewDateTimePickerColumn colClone = base.Clone() as DataGridViewDateTimePickerColumn;

            colClone.Format       = _format;
            colClone.CustomFormat = _customFormat;
            colClone.ShowUpDown   = _showUpDown;
            return(colClone);
        }