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

简介: /// <summary> /// DataGridView自定义添加DateTimePicker控件日期列 参考http://msdn.microsoft.com/en-us/library/7tas5c80.aspx /// 涂聚文 缔友计算机信息技术有限公司 /// 2011-11-16 捷为工作室 /// </summary> public cla
/// <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";
           }
       }



目录
相关文章
|
JSON JavaScript Unix
多种方式解决EasyUI—DataGrid中DateTime的格式化问题
多种方式解决EasyUI—DataGrid中DateTime的格式化问题
512 0
多种方式解决EasyUI—DataGrid中DateTime的格式化问题
|
JSON 数据库 数据格式
【datagrid】动态加载列
【datagrid】动态加载列
107 0
jqGrid 表格设置单选按钮
jqGrid 表格设置单选按钮
156 0
|
JavaScript 数据库
EasyUI_datagrid实现动态加载列并为其绑定数据
EasyUI_datagrid实现动态加载列并为其绑定数据
676 0
|
JSON JavaScript 前端开发
EasyUI-DataGrid控件[表格+日历]应用
EasyUI-DataGrid控件[表格+日历]应用
EasyUI-DataGrid控件[表格+日历]应用
使用element-ui实现日期选项卡
element-ui大家应该都知道,最近接触到一个日期选项卡的案例,顺便总结一下
298 0
c#Winform修改datatable的列的操作和一些combox绑定实体类,dataGridview的注意点 弹出确认框 弹出的winform出现的位置 load
ds是DataSet 是Datatable的集合 ds.Tables[0]是得到第一张表 然后就是对dt的操作 将Fill_ID列名修改为 “序号” 依次修改列名 combox绑定list 显示combox上的值是用cmb_name 但是 在窗体加载的时候 cmb_name是 它本身的类型名字 而不是空 只有当它上面绑定有真正的值后才会显示。
1376 0