001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.Linq;
004.
using
System.Text;
005.
using
System.Threading.Tasks;
006.
using
System.Windows.Forms;
007.
008.
namespace
DateTimeColumn
009.
{
010.
#region _GridDateControl
011.
public
class
GridDateControl : DataGridViewColumn
012.
{
013.
014.
public
GridDateControl()
015.
:
base
(
new
CalendarCell())
016.
{
017.
}
018.
019.
public
override
DataGridViewCell CellTemplate
020.
{
021.
get
{
return
base
.CellTemplate; }
022.
023.
set
024.
{
025.
026.
if
((value !=
null
) && !value.GetType().IsAssignableFrom(
typeof
(CalendarCell)))
027.
{
028.
throw
new
InvalidCastException(
"Must be a CalendarCell"
);
029.
}
030.
base
.CellTemplate = value;
031.
032.
}
033.
}
034.
035.
}
036.
037.
public
class
CalendarCell : DataGridViewTextBoxCell
038.
{
039.
040.
public
CalendarCell()
041.
{
042.
043.
}
044.
045.
public
override
void
InitializeEditingControl(
int
rowIndex,
object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
046.
{
047.
048.
base
.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
049.
050.
CalendarEditingControl ctl = (CalendarEditingControl)DataGridView.EditingControl;
051.
if
((!
object
.ReferenceEquals(
this
.Value, DBNull.Value)))
052.
{
053.
if
((
this
.Value !=
null
))
054.
{
055.
ctl.Value = (DateTime)
this
.Value;
056.
}
057.
}
058.
}
059.
060.
public
override
Type EditType
061.
{
062.
063.
get
{
return
typeof
(CalendarEditingControl); }
064.
}
065.
066.
public
override
Type ValueType
067.
{
068.
069.
get
{
return
typeof
(DateTime); }
070.
}
071.
}
072.
073.
class
CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
074.
{
075.
076.
private
DataGridView dataGridViewControl;
077.
private
bool
valueIsChanged =
false
;
078.
private
int
rowIndexNum;
079.
public
CalendarEditingControl()
080.
{
081.
this
.Format = DateTimePickerFormat.Short;
082.
}
083.
084.
public
object
EditingControlFormattedValue
085.
{
086.
get
{
return
this
.Value.ToShortDateString(); }
087.
set
088.
{
089.
if
(value
is
String)
090.
{
091.
this
.Value = DateTime.Parse(Convert.ToString(value));
092.
}
093.
}
094.
}
095.
096.
097.
public
object
GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
098.
{
099.
100.
return
this
.Value.ToShortDateString();
101.
102.
}
103.
104.
105.
public
void
ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
106.
{
107.
this
.Font = dataGridViewCellStyle.Font;
108.
this
.CalendarForeColor = dataGridViewCellStyle.ForeColor;
109.
this
.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
110.
111.
}
112.
113.
public
int
EditingControlRowIndex
114.
{
115.
116.
get
{
return
rowIndexNum; }
117.
set
{ rowIndexNum = value; }
118.
}
119.
120.
121.
public
bool
EditingControlWantsInputKey(Keys key,
bool
dataGridViewWantsInputKey)
122.
{
123.
124.
125.
switch
(key & Keys.KeyCode)
126.
{
127.
case
Keys.Left:
128.
case
Keys.Up:
129.
case
Keys.Down:
130.
case
Keys.Right:
131.
case
Keys.Home:
132.
case
Keys.End:
133.
case
Keys.PageDown:
134.
case
Keys.PageUp:
135.
136.
137.
return
true
;
138.
default
:
139.
return
false
;
140.
}
141.
142.
}
143.
144.
145.
public
void
PrepareEditingControlForEdit(
bool
selectAll)
146.
{
147.
148.
149.
}
150.
151.
public
bool
RepositionEditingControlOnValueChange
152.
{
153.
154.
get
{
return
false
; }
155.
}
156.
157.
158.
public
DataGridView EditingControlDataGridView
159.
{
160.
161.
get
{
return
dataGridViewControl; }
162.
set
{ dataGridViewControl = value; }
163.
}
164.
165.
166.
public
bool
EditingControlValueChanged
167.
{
168.
169.
get
{
return
valueIsChanged; }
170.
set
{ valueIsChanged = value; }
171.
}
172.
public
Cursor EditingControlCursor
173.
{
174.
175.
get
{
return
base
.Cursor; }
176.
}
177.
Cursor IDataGridViewEditingControl.EditingPanelCursor
178.
{
179.
get
{
return
EditingControlCursor; }
180.
}
181.
182.
protected
override
void
OnValueChanged(EventArgs eventargs)
183.
{
184.
185.
valueIsChanged =
true
;
186.
187.
this
.EditingControlDataGridView.NotifyCurrentCellDirty(
true
);
188.
base
.OnValueChanged(eventargs);
189.
}
190.
191.
}
192.
#endregion
193.
}