zoukankan
html css js c++ java
Winform下的Datagrid的列风格(3)—DataGridTimePickerColumn(转)
class
DataGridTimePickerColumn : DataGridColumnStyle
{
private
DateTimePicker myDateTimePicker
=
new
DateTimePicker();
//
The isEditing field tracks whether or not the user is
//
editing data with the hosted control.
private
bool
isEditing;
public
DataGridTimePickerColumn() :
base
()
{
myDateTimePicker.Visible
=
false
;
}
protected
override
void
Abort(
int
rowNum)
{
isEditing
=
false
;
myDateTimePicker.ValueChanged
-=
new
EventHandler(TimePickerValueChanged);
Invalidate();
}
protected
override
bool
Commit
(CurrencyManager dataSource,
int
rowNum)
{
myDateTimePicker.Bounds
=
Rectangle.Empty;
myDateTimePicker.ValueChanged
-=
new
EventHandler(TimePickerValueChanged);
if
(
!
isEditing)
return
true
;
isEditing
=
false
;
try
{
DateTime value
=
myDateTimePicker.Value;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch
(Exception)
{
Abort(rowNum);
return
false
;
}
Invalidate();
return
true
;
}
protected
override
void
Edit(
CurrencyManager source,
int
rowNum,
Rectangle bounds,
bool
readOnly,
string
instantText,
bool
cellIsVisible)
{
DateTime value
=
(DateTime)
GetColumnValueAtRow(source, rowNum);
if
(cellIsVisible)
{
myDateTimePicker.Bounds
=
new
Rectangle
(bounds.X
+
2
, bounds.Y
+
2
,
bounds.Width
-
4
, bounds.Height
-
4
);
myDateTimePicker.Value
=
value;
myDateTimePicker.Visible
=
true
;
myDateTimePicker.ValueChanged
+=
new
EventHandler(TimePickerValueChanged);
}
else
{
myDateTimePicker.Value
=
value;
myDateTimePicker.Visible
=
false
;
}
if
(myDateTimePicker.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
protected
override
Size GetPreferredSize(
Graphics g,
object
value)
{
return
new
Size(
100
, myDateTimePicker.PreferredHeight
+
4
);
}
protected
override
int
GetMinimumHeight()
{
return
myDateTimePicker.PreferredHeight
+
4
;
}
protected
override
int
GetPreferredHeight(Graphics g,
object
value)
{
return
myDateTimePicker.PreferredHeight
+
4
;
}
protected
override
void
Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int
rowNum)
{
Paint(g, bounds, source, rowNum,
false
);
}
protected
override
void
Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int
rowNum,
bool
alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected
override
void
Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int
rowNum,
Brush backBrush,
Brush foreBrush,
bool
alignToRight)
{
DateTime date
=
(DateTime)
GetColumnValueAtRow(source, rowNum);
Rectangle rect
=
bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(
0
,
2
);
rect.Height
-=
2
;
g.DrawString(date.ToString(
"
d
"
),
this
.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}
protected
override
void
SetDataGridInColumn(DataGrid value)
{
base
.SetDataGridInColumn(value);
if
(myDateTimePicker.Parent
!=
null
)
{
myDateTimePicker.Parent.Controls.Remove
(myDateTimePicker);
}
if
(value
!=
null
)
{
value.Controls.Add(myDateTimePicker);
}
}
private
void
TimePickerValueChanged(
object
sender, EventArgs e)
{
this
.isEditing
=
true
;
base
.ColumnStartedEditing(myDateTimePicker);
}
}
查看全文
相关阅读:
C艹老师布置的思考题
计蒜客练习题:网页跳转(java / C++仔细)
计蒜客练习题:水果店(嵌套map)
计蒜客练习题:蒜头君面试(map + max_element)
小希的迷宫 HDU 1272(并查集)
OpenJ_Bailian 1061
Aizu 2224(并查集)
Aizu 0189 (最短路)
POJ 2377(最大生成树)
OpenJ_Bailian 4118(dp)
原文地址:https://www.cnblogs.com/yitian/p/1289999.html
最新文章
吾日三省吾身(2)
吾日三省吾身(1)
2020年寒假学习进度第十天
2020年寒假学习进度第九天
2020年学习进度第八天
2020年寒假学习进度第七天
2020年寒假学习进度第六天
2020年寒假学习进度第五天
2020年暑假学习进度第四天
2020年暑假学习进度第三天
热门文章
2020年寒假学习进度第二天
假期学习进度第一天
50个必备的实用jQuery代码段
常用设计网站
检测浏览器内核
js过滤敏感词语
rongCloud2
种类并查集的一些理解
计蒜客练习题:n个最小和(思想+优先队列)
自定义类型的优先队列
Copyright © 2011-2022 走看看