zoukankan      html  css  js  c++  java
  • 扩展:向DataGridView 增加日历列及日历单元格

    using System;
    using System.Windows.Forms;
    
    namespace subform_TFS.Control
    {
        /// <summary> 
        /// Calendar column in DataGridView 
        /// </summary> 
        /// <example > 
        /// DataGridViewCalendarColumn col = new DataGridViewCalendarColumn(); 
        /// col.DataPropertyName = "CreateDate"; 
        /// col.HeaderText = "Calendar"; 
        /// </example> 
        public class DataGridViewCalendarColumn : DataGridViewColumn
        {
            public DataGridViewCalendarColumn()
                : base(new DataGridViewCalendarCell())
            {
            }
    
            public override DataGridViewCell CellTemplate
            {
                get
                {
                    return base.CellTemplate;
                }
                set
                {
                    // Ensure that the cell used for the template is a DataGridViewCalendarCell. 
                    if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewCalendarCell)))
                    {
                        throw new InvalidCastException("Must be a DataGridViewCalendarCell");
                    }
                    base.CellTemplate = value;
                }
            }
        }
    
        /// <summary> 
        /// Calendar cell in DataGridView 
        /// </summary> 
        public class DataGridViewCalendarCell : DataGridViewTextBoxCell
        {
            public DataGridViewCalendarCell()
                : base()
            {
                this.Style.Format = "d";
            }
    
            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);
                CalendarEditingControl ctl = DataGridView.EditingControl as CalendarEditingControl;
                            
                try
                {
                    ctl.Value = (DateTime)this.Value;
                }
                catch (Exception)
                {
                    ctl.Value = DateTime.Now;
                }
            }
    
            public override Type EditType
            {
                get
                {                
                    return typeof(CalendarEditingControl);
                }
            }
    
            public override Type ValueType
            {
                get
                {
                    return typeof(DateTime);
                }
            }
    
            public override object DefaultNewRowValue
            {
                get
                {
                    // Use the current date and time as the default value..
                    return DateTime.Now;
                }
            }
        }
    
        class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
        {
            DataGridView dataGridView; 
            private bool valueChanged = false; 
            int rowIndex;
            public CalendarEditingControl()
            {
                this.Format = DateTimePickerFormat.Short;
            }
    
            public object EditingControlFormattedValue
            {
                get
                {
                    return this.Value.ToShortDateString();
                }
                set
                {
                    String newValue = value as String;
                    if (newValue != null)
                    {
                        this.Value = DateTime.Parse(newValue);
                    }
                }
            }
    
            public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
            {
                return EditingControlFormattedValue;
            }
    
            public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
            {
                this.Font = dataGridViewCellStyle.Font;
                this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
                this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
            }
    
            public int EditingControlRowIndex
            {
                get
                {
                    return rowIndex;
                }
                set
                {
                    rowIndex = value;
                }
            }
    
            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 false;
                }
            }
    
            public void PrepareEditingControlForEdit(bool selectAll)
            {
                // No preparation needs to be done. 
            }
    
            public bool RepositionEditingControlOnValueChange
            {
                get
                {
                    return false;
                }
            }
    
            public DataGridView EditingControlDataGridView
            {
                get
                {
                    return dataGridView;
                }
                set
                {
                    dataGridView = value;
                }
            }
    
            public bool EditingControlValueChanged
            {
                get
                {
                    return valueChanged;
                }
                set
                {
                    valueChanged = value;
                }
            }
    
            public Cursor EditingPanelCursor
            {
                get
                {
                    return base.Cursor;
                }
            }
    
            protected override void OnValueChanged(EventArgs eventargs)
            {
                // Notify the DataGridView that the contents of the cell have changed. 
                valueChanged = true;
                this.EditingControlDataGridView.NotifyCurrentCellDirty(true); base.OnValueChanged(eventargs);
            }
        }
    }
    
  • 相关阅读:
    c#常用简化语句
    SQL语句使用中的笔录
    UpdateProgress 实现加载效果
    maven的下载安装,配置本地仓库
    IntelliJ IDEA启动Tomcat后,无法访问Tomcat主页
    SSM框架实现分页
    mybatis-generator-xxxx 使用方法
    Mybatis错误:Parameter 'XXX' not found. Available parameters are [1, 0, param1, param2]
    mybatis高级映射(一对一,一对多)
    SSM整合---实现全部用户查询
  • 原文地址:https://www.cnblogs.com/fery/p/2718009.html
Copyright © 2011-2022 走看看