出處:http://www.hzhcontrols.com/原文:http://www.hzhcontrols.com/blog-149.html本文版權歸www.hzhcontrols.com所有歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利 ...
出處:http://www.hzhcontrols.com/
原文:http://www.hzhcontrols.com/blog-149.html
本文版權歸www.hzhcontrols.com所有
歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利
官網
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果覺得寫的還行,請點個 star 支持一下吧
來都來了,點個【推薦】再走吧,謝謝
NuGet
Install-Package HZH_Controls
目錄
http://www.hzhcontrols.com/blog-63.html
用處及效果
因為前面寫的表格存在一些問題,本篇文章將對其優化處理,達到以下效果,支持自定義圖片和按鈕等自定義單元格
準備工作
優化是在原表格基礎上做的處理,如果不瞭解可以移步查看一下
開始
移除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 點個星星吧