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);
}
}
查看全文
相关阅读:
如何解决MathPage.wll或MathType.dll文件找不到问题
2-构建模型
R语言 ur.df函数(2)
平稳过程趋势项变点的 CUSUV 检验 秦瑞兵,郭娟
时间序列的弱相依与强相依
Cent OS|使用httpd发布网页
C++练习 | 基于栈的中缀算术表达式求值(double类型
C++练习 | 不使用头插法逆转单链表
C++练习 | 单链表的创建与输出(结构体格式)
C++练习 | 最长公共字符串(DP)
原文地址:https://www.cnblogs.com/yitian/p/1290000.html
最新文章
表达式细节问题
HQL和Criteria
Hibernate
Strurs2总结
Strut2 和Spring MVC 文件上传对比
邮箱的使用与配置
Action创建四种方式
action中请求参数获取的两种方式
简单说struts2框架运行流程
JavaScript易错点
热门文章
常用的正则表达式总结
字符串和编码
复试安排
3-代码
R语言基础知识学习(一):R中的向量的产生与访问
印刷要求
书写规定
学士学位论文撰写格式
Texlive+sublime+sumatraPDF
解决mac双系统bootcamp不能打开的问题
Copyright © 2011-2022 走看看