分享Winform datagridview 动态生成中文HeaderText
缘起
很久以前给datagridview绑定列的时候都是手动的,记得以前用Display自定义属性来动态给datagridview绑定列。后来发现不行,于是还在博问发了问题:

后来热心网友帮我回答了这个问题,一试还真可以。再次记录一下。
测试流程:
1) View Entity:
/// <summary>
/// IPC完工成品情况查询实体
/// </summary>
public class GdvSourceProductRpt
{
[DisplayName("客户名称")]
public string custerName { get; set; }
[DisplayName("成品订单号")]
public string orderNo { get; set; } [DisplayName("产品名称")]
public string InvName { get; set; } [DisplayName("制令")]
public string mo_code { get; set; } [DisplayName("物料号")]
public string goods_code { get; set; } [DisplayName("订单量")]
public int order_count { get; set; } [DisplayName("制令数")]
public int mo_count { get; set; } [DisplayName("成品产量")]
public int scanNum { get; set; } [ProgressBarCell(Style = 1)]
[DisplayName("完成度")]
public double rate { get; set; }
}
2)DataGridView的DataSource设置为窗体里的 BindingSource,DataGridView自动完成中文列名的显示
这个是手工操作按下不表
3)实现动态绑定列,实现多个数据源在一个DataGridView里显示
/// <summary>
/// 动态绑定DataGridView的列
/// </summary>
/// <param name="t"></param>
/// <param name="dgv"></param>
protected virtual void BingGridColumn(Type t,DataGridView dgv)
{
dgv.Columns.Clear();
var props = t.GetProperties().Where(prop => prop.GetCustomAttributes(typeof(DisplayNameAttribute), false).Any());
int col_width = dgv.Width / props.Count() - 8;
foreach (var prop in props)
{
DisplayNameAttribute attrib = (DisplayNameAttribute)prop.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault();
if (prop.GetCustomAttributes(typeof(ProgressBarCellAttribute), false).Any())
{
DataGridViewProgressBarColumn dc = new DataGridViewProgressBarColumn();
dc.Name = "col" + prop.Name;
dc.HeaderText = attrib.DisplayName;
dc.DataPropertyName = prop.Name;
dc.Width = col_width;
dgv.Columns.Add(dc);
}
else
{
DataGridViewTextBoxColumn dc = new DataGridViewTextBoxColumn();
dc.Name = "col" + prop.Name;
dc.HeaderText = attrib.DisplayName;
dc.DataPropertyName = prop.Name;
dc.Width = col_width;
dgv.Columns.Add(dc);
}
}
}
使用:

效果:

顺便贴一下代码,如何实现DataGridView进度条列。
public class DataGridViewProgressBarCell : DataGridViewCell
{
public DataGridViewProgressBarCell()
{
this.ValueType = typeof(decimal);
} //设置进度条的背景色;
public DataGridViewProgressBarCell(Color progressBarColor)
: base()
{
ProgressBarColor = progressBarColor;
} protected Color ProgressBarColor = Color.Green; //进度条的默认背景颜色,绿色;
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
using (SolidBrush backBrush = new SolidBrush(cellStyle.BackColor))
{
graphics.FillRectangle(backBrush, cellBounds);
}
base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
using (SolidBrush brush = new SolidBrush(ProgressBarColor))
{
if (value == null)
value = 0;
decimal num = System.Convert.ToDecimal(value);
float percent = (float)(num / System.Convert.ToDecimal(100));
graphics.FillRectangle(brush, cellBounds.X, cellBounds.Y + 1, cellBounds.Width * percent, cellBounds.Height - 3);
string text = string.Format("{0}%", num);
SizeF rf = graphics.MeasureString(text, cellStyle.Font);
float x = cellBounds.X + (cellBounds.Width - rf.Width) / 2f;
float y = cellBounds.Y + (cellBounds.Height - rf.Height) / 2f;
Color txtColor = Color.Lime;
if (System.Convert.ToDecimal(value) < 10)
txtColor = Color.OrangeRed;
if (System.Convert.ToDecimal(value) < 50 && System.Convert.ToDecimal(value) > 20)
txtColor = Color.DarkBlue;
graphics.DrawString(text, cellStyle.Font, new SolidBrush(txtColor), x, y);
}
}
}
public class DataGridViewProgressBarColumn : DataGridViewColumn
{
public DataGridViewProgressBarColumn()
: base(new DataGridViewProgressBarCell())
{
CellTemplate = new DataGridViewProgressBarCell();
}
}
分享Winform datagridview 动态生成中文HeaderText的更多相关文章
- winform中动态生成多行label,同时添加滚动条
设计思路大概是这样的,Form内添加一个groupBox,groupBox内添加一个panel,panel的属性AutoScroll=true,在panel内动态添加label. 原始From如下: ...
- [WinForm]dataGridView动态加载以本地图片显示列
增加一个图片列: C# private void btnQuery_Click(object sender, EventArgs e) { StringBuilder sb=new StringBui ...
- 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续3篇-导出时动态生成多Sheet EXCEL)
ExcelUtility 类库经过我(梦在旅途)近期不断的优化与新增功能,现已基本趋向稳定,功能上也基本可以满足绝大部份的EXCEL导出需求,该类库已在我们公司大型ERP系统全面使用,效果不错,今天应 ...
- 动态生成二维码并利用canvas合成出一张图片(类似海报、分享页)
在前端开发并打算推广一个APP的时候,推广页是免不了的,而推广页的展示方式一般是给人家一个二维码,让别人自己去安装APP,这样前段任务也达到了,这次写这篇文章的原因主要还是总结一下,其中有很多不完善的 ...
- 动态生成CheckBox(Winform程序)
在做用户权限设置功能时,需要做一个动态生成权限列表的功能.(笔记.分享) //1.清空权限控件组的默认控件 panelPermissions.Controls.Clear(); _groupBoxLi ...
- winform WebBrowser控件中,cs后台代码执行动态生成的js
很多文章都是好介绍C# 后台cs和js如何交互,cs调用js方法(js方法必须是页面上存在的,已经定义好的),js调用cs方法, 但如果想用cs里面执行动态生成的js代码,如何实现呢? 思路大致是这样 ...
- C#实现WinForm DataGridView控件支持叠加数据绑定
我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...
- Java 动态生成 复杂 .doc文件
阅读目录 1.word 里面调整好排版,包括你想生成的动态部分,还有一些不用生成的规则性的文字 2. 将 word 文档保存为 xml 3.用 Firstobject free XML edito 打 ...
- 基于DevExpress的BandedGridView动态生成多行(复合)表头
最近cs项目中有个看板的功能需求,整个系统是基于DevExpress组件开发的,由于对这个组件的布局不是很熟,也借鉴了网上一些其他人的做法,普遍都是通过GridControl的BandedGridVi ...
随机推荐
- 痞子衡嵌入式:ARM Cortex-M调试那些事(1)- 4线协议标准(JTAG)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是嵌入式调试里的接口标准JTAG. 在结束<ARM Cortex-M文件那些事>系列文章之后,痞子衡休整了一小段时间,但是讲课的 ...
- Spring基础系列-参数校验
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9953744.html Spring中使用参数校验 概述 JSR 303中提出了Bea ...
- React组件方法中为什么要绑定this
如果你尝试使用过React进行前端开发,一定见过下面这样的代码: //假想定义一个ToggleButton开关组件 class ToggleButton extends React.Component ...
- .Net语言 APP开发平台——Smobiler学习日志:如何快速实现按钮组功能
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的“Smobil ...
- js如何使用正则表达式实现过滤HTML标签?(/<[^<>]+>/g)
js如何使用正则表达式实现过滤HTML标签?(/<[^<>]+>/g) 一.总结 js进阶正则表达式实现过滤HTML标签(<>标签中不能包含标签实现过滤HTML标签 ...
- webpack打包vue
一.原理 webpack 背后的原理其实就是把所有的非 js 资源都转换成 js (如把一个 css 文件转换成“创建一个 style 标签并把它插入 document ”的脚本.把图片转换成一个图片 ...
- HTML和CSS在IE7中常见的兼容性问题
IE7及以下版本都会有这些问题 1.IE7块转内联块问题 问题描述:好像块转内联块失败,依然不在一行排列 解决办法:给元素添加如下css *display:inline;*zoom:1; *di ...
- Azure WebJob-Custom Schedule for Azure Web Job Timer Triggers
如果想实现Azure Schedule WebJob,有两种方法: 1. 配置CRON Expression,网上有在线CRON配置工具,根据业务需要配置即可 注意:Azure的CRON Expres ...
- C#字符串倒置函数的代码
把内容过程比较常用的内容珍藏起来,下边内容内容是关于C#字符串倒置函数的内容. public static string Reverse(string ReverseString) { String ...
- chart 模板 - 每天5分钟玩转 Docker 容器技术(165)
Helm 通过模板创建 Kubernetes 能够理解的 YAML 格式的资源配置文件,我们将通过例子来学习如何使用模板. 以 templates/secrets.yaml 为例: 从结构看,文件的内 ...