出處:http://www.hzhcontrols.com/
原文:http://www.hzhcontrols.com/blog-149.html
本文版權歸www.hzhcontrols.com全部
歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利html
http://www.hzhcontrols.com/git
入行已經7,8年了,一直想作一套漂亮點的自定義控件,因而就有了本系列文章。github
GitHub:https://github.com/kwwwvagaa/NetWinformControlc#
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git優化
若是以爲寫的還行,請點個 star 支持一下吧this
來都來了,點個【推薦】再走吧,謝謝code
Install-Package HZH_Controls
http://www.hzhcontrols.com/blog-63.htmlorm
由於前面寫的表格存在一些問題,本篇文章將對其優化處理,達到如下效果,支持自定義圖片和按鈕等自定義單元格htm
優化是在原表格基礎上作的處理,若是不瞭解能夠移步查看一下
移除UCDataGridView中全部自適應高度相關的功能,移除分頁控件
DataGridViewColumnEntity中添加自定義單元格屬性
/// <summary>
/// 自定義的單元格控件,一個實現IDataGridViewCustomCell的Control
/// </summary>
/// <value>The custom cell.</value>
private
Type customCellType =
null
;
public
Type CustomCellType
{
get
{
return
customCellType;
}
set
{
if
(!
typeof
(IDataGridViewCustomCell).IsAssignableFrom(value) || !value.IsSubclassOf(
typeof
(System.Windows.Forms.Control)))
throw
new
Exception(
"行控件沒有實現IDataGridViewCustomCell接口"
);
customCellType = value;
}
}
|
行控件綁定自定義行
if
(item.CustomCellType ==
null
)
{
Label lbl =
new
Label();
lbl.Tag = i - (IsShowCheckBox ? 1 : 0);
lbl.Name =
"lbl_"
+ item.DataField;
lbl.Font =
new
Font(
"微軟雅黑"
, 12);
lbl.ForeColor = Color.Black;
lbl.AutoSize =
false
;
lbl.Dock = DockStyle.Fill;
lbl.TextAlign = item.TextAlign;
lbl.MouseDown += (a, b) =>
{
Item_MouseDown(a, b);
};
c = lbl;
}
else
{
Control cc = (Control)Activator.CreateInstance(item.CustomCellType);
cc.Dock = DockStyle.Fill;
c = cc;
}
|
支持基本上就完成了所有的控制了,而後看下調用示例
List<DataGridViewColumnEntity> lstCulumns =
new
List<DataGridViewColumnEntity>();
lstCulumns.Add(
new
DataGridViewColumnEntity() { Width = 35, WidthType = SizeType.Absolute, CustomCellType =
typeof
(UCTestGridTable_CustomCellIcon) });
lstCulumns.Add(
new
DataGridViewColumnEntity() { DataField =
"ID"
, HeadText =
"編號"
, Width = 70, WidthType = SizeType.Absolute });
lstCulumns.Add(
new
DataGridViewColumnEntity() { DataField =
"Name"
, HeadText =
"姓名"
, Width = 50, WidthType = SizeType.Percent });
lstCulumns.Add(
new
DataGridViewColumnEntity() { DataField =
"Age"
, HeadText =
"年齡"
, Width = 50, WidthType = SizeType.Percent });
lstCulumns.Add(
new
DataGridViewColumnEntity() { DataField =
"Birthday"
, HeadText =
"生日"
, Width = 50, WidthType = SizeType.Percent, Format = (a) => {
return
((DateTime)a).ToString(
"yyyy-MM-dd"
); } });
lstCulumns.Add(
new
DataGridViewColumnEntity() { DataField =
"Sex"
, HeadText =
"性別"
, Width = 50, WidthType = SizeType.Percent, Format = (a) => {
return
((
int
)a) == 0 ?
"女"
:
"男"
; } });
lstCulumns.Add(
new
DataGridViewColumnEntity() { Width = 155, WidthType = SizeType.Absolute,CustomCellType=
typeof
(UCTestGridTable_CustomCell) });
this
.ucDataGridView1.Columns = lstCulumns;
this
.ucDataGridView1.IsShowCheckBox =
true
;
List<
object
> lstSource =
new
List<
object
>();
for
(
int
i = 0; i < 50; i++)
{
TestGridModel model =
new
TestGridModel()
{
ID = i.ToString(),
Age = 3 * i,
Name =
"姓名——"
+ i,
Birthday = DateTime.Now.AddYears(-10),
Sex = i % 2
};
lstSource.Add(model);
}
this
.ucDataGridView1.DataSource = lstSource;
|
若是你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧