Csharp DataGridView自定义添加DateTimePicker控件日期列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | /// <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); } } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //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" ; } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
DataGridView
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!