(八十六)c#Winform自定义控件-表格优化
出处: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 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 
来都来了,点个【推荐】再走吧,谢谢
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 点个星星吧
(八十六)c#Winform自定义控件-表格优化的更多相关文章
- (八十)c#Winform自定义控件-分割线标签-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- 《手把手教你》系列基础篇(八十六)-java+ selenium自动化测试-框架设计基础-Log4j实现日志输出(详解教程)
1.简介 自动化测试中如何输出日志文件.任何软件,都会涉及到日志输出.所以,在测试人员报bug,特别是崩溃的bug,一般都要提供软件产品的日志文件.开发通过看日志文件,知道这个崩溃产生的原因,至少知道 ...
- 第三百八十六节,Django+Xadmin打造上线标准的在线教育平台—HTML母版继承
第三百八十六节,Django+Xadmin打造上线标准的在线教育平台—HTML母版继承 母板-子板-母板继承 母板继承就是访问的页面继承一个母板,将访问页面的内容引入到母板里指定的地方,组合成一个新页 ...
- (三十二)c#Winform自定义控件-表格
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二十)c#Winform自定义控件-有后退的窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (五十)c#Winform自定义控件-滑块
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (六十)c#Winform自定义控件-鼓风机(工业)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- Java面试准备十六:数据库——MySQL性能优化
2017年04月20日 13:09:43 阅读数:6837 这里只是为了记录,由于自身水平实在不怎么样,难免错误百出,有错的地方还望大家多多指出,谢谢. 来自MySQL性能优化的最佳20+经验 为查询 ...
随机推荐
- node学习笔记(二)流和缓冲区
内容 视频 第四章内容 菜鸟教程服务器 //复制文件 function de(x) { console.log(x); } var fs=require('fs'); fs.mkdir('stuff' ...
- Shell之命令执行的判断依据
目录 Shell之命令执行的判断依据 参考 Shell之命令执行的判断依据
- 第一篇:php开发环境
Window: 1.XAMPPhttps://www.apachefriends.org/index.html 2.WampServerhttp://www.wampserver.com/ Linux ...
- Scala 学习笔记之函数(2)
class OldStudent extends Student { def filterName(s: String, f: String => String) = { if (s != nu ...
- Scala 学习笔记之函数(1)
class Student { val mySayHello1: String => Unit = sayHello val mySayHello2: (String, Int) => U ...
- requests模块(post)请求篇
'''利用parse模块模拟post请求分析百度词典分析步骤:1. 打开F122. 尝试输入单词girl,发现每敲一个字母后都有请求3. 请求地址是 http://fanyi.baidu.com/su ...
- 代码审计-YXcms1.4.7
题外: 今天是上班第一天,全都在做准备工作,明天开始正式实战做事. 看着周围稍年长的同事和老大做事,自己的感觉就是自己还是差的很多很多,自己只能算个废物. 学无止境,我这样的垃圾废物就该多练,保持战斗 ...
- Circle Problem From 3Blue1Brown (分圆问题)
Background\text{Background}Background Last night, lots of students from primary school came to our c ...
- 阻塞IO模型
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> # ...
- MFC::使用mysql
下载mysql-installer-community-5.7.16.0.msi,安装 mysql server即可. 创建工程包含头文件 #include "winsock.h" ...