(八十六)c#Winform自定义控件-表格优化
程序员文章站
2022-06-07 12:20:45
出处:http://www.hzhcontrols.com/原文:http://www.hzhcontrols.com/blog-149.html本文版权归www.hzhcontrols.com所有欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利 ......
出处:
原文:
本文版权归www.hzhcontrols.com所有
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
github:https://github.com/kwwwvagaa/netwinformcontrol
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
来都来了,点个【推荐】再走吧,谢谢
nuget
install-package hzh_controls
目录
用处及效果
因为前面写的表格存在一些问题,本篇文章将对其优化处理,达到以下效果,支持自定义图片和按钮等自定义单元格
准备工作
优化是在原表格基础上做的处理,如果不了解可以移步查看一下
开始
移除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;
|
最后的话
如果你喜欢的话,请到 点个星星吧