Пример #1
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));
        }