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);
}
}
查看全文
相关阅读:
linux的vim按了ctrl+s之后假死的解决办法
linux下的终端模拟器urxvt的配置
vim下正则表达式的非贪婪匹配
linux中的一个看图的软件
解决windows的控制台显示utf8乱码的问题
[PHP][位转换积累]之异或运算的简单加密应用
[PHP][REDIS]phpredis 'RedisException' with message 'read error on connection'
[PHP][位转换积累]之与运算截取二进制流的值
[PHP][位转换积累]之pack和unpack
[正则表达式]PCRE反向分组引用
原文地址:https://www.cnblogs.com/yitian/p/1290000.html
最新文章
203. Remove Linked List Elements【easy】
82. Remove Duplicates from Sorted List II【Medium】
83. Remove Duplicates from Sorted List【easy】
21. Merge Two Sorted Lists【easy】
142. Linked List Cycle II【easy】
[待解决] sudo unable to resolve host
编译php时的一个脚本
编译php时出现xsl错误的解决方法
使用sudo执行命令的时候提示找不到命令
linux下创建的符号链接的权限
热门文章
记linux下使用create_ap 创建热点失败及解决(涉及rfkill)
在linux中搭建git服务器
linux中查看硬件温度的命令
linux时区的设置
linux系统的时间调整
[链接] github中维护的一个gitignore项目
linux中编译git时提示找不到ssl.h头文件
在linux下的firefox中安装flashplayer
linux下调整音量大小
我使用的vim配置文件
Copyright © 2011-2022 走看看