zoukankan
html css js c++ java
Winform下的Datagrid的列风格(4)—DataGridComboBoxTableViewColumn
class
DataGridComboBoxTableViewColumn: DataGridColumnStyle
{
//
与DataGridComboBoxColumn不同,显示时使用DataTable,下拉时使用DataView
private
ComboBox myComboBox
=
new
ComboBox ();
private
DataView ChoiceDataViewSource;
private
DataView TableDataViewSource;
private
string
ChoiceDisplayField;
private
string
ChoiceValueField;
private
bool
isEditing;
public
DataGridComboBoxTableViewColumn(DataView ChoiceDataViewSource,
string
ChoiceDisplayField,
string
ChoiceValueField) :
base
()
{
this
.ChoiceDataViewSource
=
ChoiceDataViewSource;
TableDataViewSource
=
new
System.Data .DataView (ChoiceDataViewSource.Table);
this
.ChoiceDisplayField
=
ChoiceDisplayField;
this
.ChoiceValueField
=
ChoiceValueField;
myComboBox.DropDownStyle
=
System.Windows .Forms .ComboBoxStyle .DropDownList ;
myComboBox.Visible
=
false
;
myComboBox.DataSource
=
this
.ChoiceDataViewSource;
myComboBox.DisplayMember
=
this
.ChoiceDisplayField;
myComboBox.ValueMember
=
this
.ChoiceValueField;
}
protected
override
void
Abort(
int
rowNum)
{
isEditing
=
false
;
myComboBox.SelectedIndexChanged
-=
new
EventHandler(ComboBoxSelectedIndexChanged );
Invalidate();
}
protected
override
bool
Commit
(CurrencyManager dataSource,
int
rowNum)
{
myComboBox.Bounds
=
Rectangle.Empty;
myComboBox.SelectedIndexChanged
-=
new
EventHandler(ComboBoxSelectedIndexChanged );
if
(
!
isEditing)
return
true
;
isEditing
=
false
;
try
{
System.Object value
=
myComboBox.SelectedValue;
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)
{
object
value
=
GetColumnValueAtRow(source, rowNum);
if
(cellIsVisible)
{
myComboBox.Bounds
=
new
Rectangle
(bounds.X
+
2
, bounds.Y
+
2
,
bounds.Width
-
4
, bounds.Height
-
4
);
int
i
=
Find (value);
if
(myComboBox.Items .Count
>
i)
{
myComboBox.SelectedIndex
=
i;
}
myComboBox.Visible
=
true
;
myComboBox.SelectedIndexChanged
+=
new
EventHandler(ComboBoxSelectedIndexChanged );
}
else
{
myComboBox.SelectedIndex
=
this
.Find (value);
myComboBox.Visible
=
false
;
}
if
(myComboBox.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
protected
int
Find(System.Object value)
{
int
int1;
int
int2;
int2
=
TableDataViewSource.Count ;
if
(int2
<=
0
)
{
return
-
1
;
}
for
(int1
=
0
;int1
<
int2;int1
++
)
{
if
(TableDataViewSource[int1][
this
.myComboBox .ValueMember ].ToString ().Trim ()
==
value.ToString ().Trim ())
{
return
int1;
}
}
return
-
1
;
}
protected
override
Size GetPreferredSize(
Graphics g,
object
value)
{
return
new
Size(
100
, myComboBox.PreferredHeight
+
4
);
}
protected
override
int
GetMinimumHeight()
{
return
myComboBox.PreferredHeight
+
4
;
}
protected
override
int
GetPreferredHeight(Graphics g,
object
value)
{
return
myComboBox.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)
{
object
value
=
GetColumnValueAtRow(source, rowNum);
int
int1
=
Find (value);
string
display
=
""
;
if
(int1
>=
0
)
{
display
=
this
.TableDataViewSource [int1][
this
.myComboBox .DisplayMember ].ToString ();
}
Rectangle rect
=
bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(
0
,
2
);
rect.Height
-=
2
;
g.DrawString(display,
this
.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}
protected
override
void
SetDataGridInColumn(DataGrid value)
{
base
.SetDataGridInColumn(value);
if
(myComboBox.Parent
!=
null
)
{
myComboBox.Parent.Controls.Remove
(myComboBox);
}
if
(value
!=
null
)
{
value.Controls.Add(myComboBox);
}
}
private
void
ComboBoxSelectedIndexChanged(
object
sender, EventArgs e)
{
this
.isEditing
=
true
;
base
.ColumnStartedEditing(myComboBox);
}
}
查看全文
相关阅读:
shell 遍历当前目录以及所有子目录下文件
linux shell 字符串操作(长度,查找,替换)详解
将自定义结构存入std::set 或者 std::map
CentOS下搭建SVN服务器
基于Debian的linux系统软件安装命令
rm搭配grep删除符合条件的文件
Centos系统环境
怎么恢复用mysqldump备份数据和恢复数据
mac 安装 node.js 的 canvas
centos 6.5 安装 node.js 的 canvas模块
原文地址:https://www.cnblogs.com/yitian/p/1290000.html
最新文章
ByPass CORS
Angular Route usage turorial
Note About WebAPI in ABP
正则表达式中test方法的使用
移动端——论使用图片撑出模拟背景所带来的好处(主要解决图片之上是一些动态变化的内容,图片的效果难以使用程序来实现)
关于对延迟加载插件的使用
前端开发经验总结
Ubuntu ssh 服务
ubuntu12.04 修改 主机名(hostname)
IO同步、异步与阻塞、非阻塞
热门文章
行测
堆和栈的区别
常用vim设置
Image zImage uImage
Fliwer:监控植物状态 实现远程浇水
嵌入式Linux内核I2C子系统详解
shell编程之——cat /dev/null作用
TCP的滑动窗口算法
TCP协议中的seq/ack序号是如何变化的?
Shell if 条件判断
Copyright © 2011-2022 走看看