Csharp DataGridView自定义添加DateTimePicker控件日期列

来源:互联网 发布:网络电视直播系统 编辑:程序博客网 时间:2024/06/06 06:52
/// <summary> /// DataGridView自定义添加DateTimePicker控件日期列 参考http://msdn.microsoft.com/en-us/library/7tas5c80.aspx /// 涂聚文 缔友计算机信息技术有限公司 /// 2011-11-16 捷为工作室 /// </summary> public class GeovinDuCalendarColumn : DataGridViewColumn {     /// <summary>     ///      /// </summary>     public GeovinDuCalendarColumn()         : base(new CalendarCell())     {     }     /// <summary>     ///      /// </summary>     public override DataGridViewCell CellTemplate     {         get         {             return base.CellTemplate;         }         set         {                       if (value != null &&                 !value.GetType().IsAssignableFrom(typeof(CalendarCell)))             {                 throw new InvalidCastException("Must be a CalendarCell");             }             base.CellTemplate = value;         }     } } /// <summary> /// DataGridView 添加日期列 /// 涂聚文 缔友计算机信息技术有限公司 /// 2011-11-16 捷为工作室 /// </summary> public class CalendarCell : DataGridViewTextBoxCell {     public CalendarCell()         : base()     {              this.Style.Format = "d";     }     public override void InitializeEditingControl(int rowIndex, object         initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)     {         base.InitializeEditingControl(rowIndex, initialFormattedValue,             dataGridViewCellStyle);         CalendarEditingControl ctl =             DataGridView.EditingControl as CalendarEditingControl;            if (this.Value == null)         {             ctl.Value = (DateTime)this.DefaultNewRowValue;         }         else         {             ctl.Value = (DateTime)this.Value;         }     }     public override Type EditType     {         get         {                        return typeof(CalendarEditingControl);         }     }     public override Type ValueType     {         get         {                        return typeof(DateTime);         }     }     public override object DefaultNewRowValue     {         get         {                      return DateTime.Now;         }     } } /// <summary> ///DataGridView 添加日期列 /// 涂聚文 缔友计算机信息技术有限公司 /// 2011-11-16 捷为工作室 /// </summary> 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         {             if (value is String)             {                 try                 {                     this.Value = DateTime.Parse((String)value);                 }                 catch                 {                                this.Value = DateTime.Now;                 }             }         }     }     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 !dataGridViewWantsInputKey;         }     }     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)     {         valueChanged = true;         this.EditingControlDataGridView.NotifyCurrentCellDirty(true);         base.OnValueChanged(eventargs);     } }

调用:

           //dataGridView1.Columns[3].HeaderText = "入職日期";           GeovinDuCalendarColumn col = new GeovinDuCalendarColumn();           this.dataGridView1.Columns.Insert(3,col);           col.HeaderText = "入職日期";/// <summary>       /// 设置默认值       /// </summary>       /// <param name="sender"></param>       /// <param name="e"></param>       private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)       {           if (dataGridView1.Rows.Count >= 1) //设定默认日期           {               this.dataGridView1.Rows[e.RowIndex].Cells[2].Value = "20E8C162-C09C-4F7A-9C97-0CA50E201F6B";               this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DateTime.Now;               this.dataGridView1.Rows[e.RowIndex].Cells[4].Value = "C50E08D5-7529-4F86-966E-9497AD67EA0C";           }       }



原创粉丝点击