zoukankan      html  css  js  c++  java
  • DataGridView控件内建立日期选择编辑列

    两个文件:

    CalendarColumn 类:

    1. public class CalendarColumn : DataGridViewColumn  
    2.     {  
    3.         public CalendarColumn()  
    4.             : base(new CalendarCell())  
    5.         {  
    6.         }  
    7.   
    8.         public override DataGridViewCell CellTemplate  
    9.         {  
    10.             get  
    11.             {  
    12.                 return base.CellTemplate;  
    13.             }  
    14.             set  
    15.             {  
    16.                 // Ensure that the cell used for the template is a CalendarCell.  
    17.                 if (value != null &&  
    18.                     !value.GetType().IsAssignableFrom(typeof(CalendarCell)))  
    19.                 {  
    20.                     throw new InvalidCastException("Must be a CalendarCell");  
    21.                 }  
    22.                 base.CellTemplate = value;  
    23.             }  
    24.         }  
    25.     }  

    **********************************************************************

    CalendarCell 类:

    1. public class CalendarCell : DataGridViewTextBoxCell  
    2.     {  
    3.   
    4.         public CalendarCell()  
    5.             : base()  
    6.         {  
    7.             // Use the short date format.  
    8.             this.Style.Format = "d";  
    9.         }  
    10.   
    11.         public override void InitializeEditingControl(int rowIndex, object  
    12.             initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)  
    13.         {  
    14.             // Set the value of the editing control to the current cell value.  
    15.             base.InitializeEditingControl(rowIndex, initialFormattedValue,  
    16.                 dataGridViewCellStyle);  
    17.             CalendarEditingControl ctl =  
    18.                 DataGridView.EditingControl as CalendarEditingControl;  
    19.             if (this.Value == null)  
    20.                 ctl.Value = DateTime.Now;  
    21.             else  
    22.                 ctl.Value = (DateTime)this.Value;  
    23.         }  
    24.   
    25.         public override Type EditType  
    26.         {  
    27.             get  
    28.             {  
    29.                 // Return the type of the editing contol that CalendarCell uses.  
    30.                 return typeof(CalendarEditingControl);  
    31.             }  
    32.         }  
    33.   
    34.         public override Type ValueType  
    35.         {  
    36.             get  
    37.             {  
    38.                 // Return the type of the value that CalendarCell contains.  
    39.                 return typeof(DateTime);  
    40.             }  
    41.         }  
    42.   
    43.         public override object DefaultNewRowValue  
    44.         {  
    45.             get  
    46.             {  
    47.                 // Use the current date and time as the default value.  
    48.                 return DateTime.Now;  
    49.             }  
    50.         }  
    51.     }  
    52.   
    53.     class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl  
    54.     {  
    55.         DataGridView dataGridView;  
    56.         private bool valueChanged = false;  
    57.         int rowIndex;  
    58.   
    59.         public CalendarEditingControl()  
    60.         {  
    61.             this.Format = DateTimePickerFormat.Short;  
    62.         }  
    63.   
    64.         // Implements the IDataGridViewEditingControl.EditingControlFormattedValue   
    65.         // property.  
    66.         public object EditingControlFormattedValue  
    67.         {  
    68.             get  
    69.             {  
    70.                 return this.Value.ToShortDateString();  
    71.             }  
    72.             set  
    73.             {  
    74.                 String newValue = value as String;  
    75.                 if (newValue != null)  
    76.                 {  
    77.                     this.Value = DateTime.Parse(newValue);  
    78.                 }  
    79.             }  
    80.         }  
    81.   
    82.         // Implements the   
    83.         // IDataGridViewEditingControl.GetEditingControlFormattedValue method.  
    84.         public object GetEditingControlFormattedValue(  
    85.             DataGridViewDataErrorContexts context)  
    86.         {  
    87.             return EditingControlFormattedValue;  
    88.         }  
    89.   
    90.         // Implements the   
    91.         // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.  
    92.         public void ApplyCellStyleToEditingControl(  
    93.             DataGridViewCellStyle dataGridViewCellStyle)  
    94.         {  
    95.             this.Font = dataGridViewCellStyle.Font;  
    96.             this.CalendarForeColor = dataGridViewCellStyle.ForeColor;  
    97.             this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;  
    98.         }  
    99.   
    100.         // Implements the IDataGridViewEditingControl.EditingControlRowIndex   
    101.         // property.  
    102.         public int EditingControlRowIndex  
    103.         {  
    104.             get  
    105.             {  
    106.                 return rowIndex;  
    107.             }  
    108.             set  
    109.             {  
    110.                 rowIndex = value;  
    111.             }  
    112.         }  
    113.   
    114.         // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey   
    115.         // method.  
    116.         public bool EditingControlWantsInputKey(  
    117.             Keys key, bool dataGridViewWantsInputKey)  
    118.         {  
    119.             // Let the DateTimePicker handle the keys listed.  
    120.             switch (key & Keys.KeyCode)  
    121.             {  
    122.                 case Keys.Left:  
    123.                 case Keys.Up:  
    124.                 case Keys.Down:  
    125.                 case Keys.Right:  
    126.                 case Keys.Home:  
    127.                 case Keys.End:  
    128.                 case Keys.PageDown:  
    129.                 case Keys.PageUp:  
    130.                     return true;  
    131.                 default:  
    132.                     return false;  
    133.             }  
    134.         }  
    135.   
    136.         // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit   
    137.         // method.  
    138.         public void PrepareEditingControlForEdit(bool selectAll)  
    139.         {  
    140.             // No preparation needs to be done.  
    141.         }  
    142.   
    143.         // Implements the IDataGridViewEditingControl  
    144.         // .RepositionEditingControlOnValueChange property.  
    145.         public bool RepositionEditingControlOnValueChange  
    146.         {  
    147.             get  
    148.             {  
    149.                 return false;  
    150.             }  
    151.         }  
    152.   
    153.         // Implements the IDataGridViewEditingControl  
    154.         // .EditingControlDataGridView property.  
    155.         public DataGridView EditingControlDataGridView  
    156.         {  
    157.             get  
    158.             {  
    159.                 return dataGridView;  
    160.             }  
    161.             set  
    162.             {  
    163.                 dataGridView = value;  
    164.             }  
    165.         }  
    166.   
    167.         // Implements the IDataGridViewEditingControl  
    168.         // .EditingControlValueChanged property.  
    169.         public bool EditingControlValueChanged  
    170.         {  
    171.             get  
    172.             {  
    173.                 return valueChanged;  
    174.             }  
    175.             set  
    176.             {  
    177.                 valueChanged = value;  
    178.             }  
    179.         }  
    180.   
    181.         // Implements the IDataGridViewEditingControl  
    182.         // .EditingPanelCursor property.  
    183.         public Cursor EditingPanelCursor  
    184.         {  
    185.             get  
    186.             {  
    187.                 return base.Cursor;  
    188.             }  
    189.         }  
    190.   
    191.         protected override void OnValueChanged(EventArgs eventargs)  
    192.         {  
    193.             // Notify the DataGridView that the contents of the cell  
    194.             // have changed.  
    195.             valueChanged = true;  
    196.             this.EditingControlDataGridView.NotifyCurrentCellDirty(true);  
    197.             base.OnValueChanged(eventargs);  
    198.         }  
    199.     }  

    *****************************************************************

    调用,和DataGridViewTextBoxColumn一样

        private CalendarColumn awardsDate;

        this.awardsDate = new CalendarColumn();

        this.awardsDate.DataPropertyName = "awardsDate";
        this.awardsDate.HeaderText = "颁奖日期";
        this.awardsDate.Name = "awardsDate";

        this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.awardsDate});

    可以新增、赋值、编辑该列。

  • 相关阅读:
    【Educational Codeforces Round 36 C】 Permute Digits
    【Educational Codeforces Round 36 B】Browser
    【Educational Codeforces Round 36 A】 Garden
    【习题 8-14 UVA
    【习题 8-13 UVA
    【习题 8-12 UVA
    【习题 8-11 UVA
    【习题 8-10 UVA
    关于货仓选址问题的方法及证明(在数轴上找一点使得该点到所有其他点的距离之和最小)
    P2512 [HAOI2008]糖果传递
  • 原文地址:https://www.cnblogs.com/wanzhongjun/p/6250073.html
Copyright © 2011-2022 走看看