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);
}
}
查看全文
相关阅读:
oracle 锁表查看与解锁
oracle 监视索引是否使用
oracle 查看数据库版本
mybatis 中文做参数报错
windows mysql绿色版配置
maven使用本地jar包
Java 使用Jedis连接Redis数据库(-)
Linux 安装redis
ELK之在CentOS7.5上使用rpm包安装配置ELK7版本
Ubuntu-18.04更改安装源为国内源
原文地址:https://www.cnblogs.com/yitian/p/1290000.html
最新文章
Bootstrap之表格checkbox复选框全选
c# Dictionary的遍历和排序
史上最详细的CocoaPods安装教程
OS10.11系统下 安装cocoapods 以及 安装cocoapods-xcode-plugin-master插件来加载三方框架
cocoapods for xcode怎么用
PHP网页缓存技术
修改apache上传文件大小限制
iOS关于UILabel 基本属性 背景图片 背景色
linux(六)之文本操作
linux(七)之linux系统中查找文件
热门文章
linux(四)之元字符
linux(五)之vi编译器
linux(三)之linux常用命令二
linux(二)之linux常用命令一
linux(一)之linux简介
Eclipse快捷键
JavaSE(七)之内部类
Java多线程(一)
oracle in 多个字段
oracle 分组中排序(rank函数)
Copyright © 2011-2022 走看看