分享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 ...
随机推荐
- Java集合类:"随机访问" 的RandomAccess接口
引出RandomAccess接口 如果我们用Java做开发的话,最常用的容器之一就是List集合了,而List集合中用的较多的就是ArrayList 和 LinkedList 两个类,这两者也常被用来 ...
- Linux下GitLab服务器搭建
系统环境 操作系统:CentOS6.9关闭防火墙 安装步骤 1. 安装Postfix 2. 下载rpm包并安装 3. 配置gitlab,vim /etc/gitlab/gitlab.rb,指定ip+端 ...
- CSS代码片段
定位: 将元素居中 将元素水平居中 将元素垂直居中 样式: 文字毛玻璃效果 -------------------------------------------代码----------------- ...
- iOS---------显示和隐藏状态栏的网络活动标志
//在向服务端发送请求状态栏显示网络活动标志: [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; ...
- Android下获取FPS的几种方法
FPS(Frames Per Second)是关乎Android用户体验最为重要的指标之一,而在VR中更是如此.为了评估VR系统.VR SDK及Unity应用的性能,通常会实时获取FPS并将其显示出来 ...
- 安卓开发:UI组件-RadioButton和复选框CheckBox
2.5RadioButton 为用户提供由两个及以上互斥的选项组成的选项集. 2.5.1精简代码 在按钮变多之后,多次重复书写点击事件有些繁琐,我们在这里创建一个事件OnClick,每次点击时调用该事 ...
- C#实现完整的防盗自制监控系统
在您的手机中通知您家中的入侵者,并拍摄他们的照片 介绍 在本文中,我将展示一些DIY东西,用于安装监控系统,检测家中的入侵者,拍摄照片并通过手机通知您,必要时可以打电话给警察并提供照片以便快速识别 ...
- 如何利用Vagrant快速搭建相同配置的开发环境?
作为一名程序猿,我们常常会遇到需要搭建开发环境的问题,特别是在新入职的时候,sublime, node, apache, mysql, php等等以及各种框架的安装.如果入职的是大公司有可能这些必要的 ...
- 从 RAID 到 Hadoop Hdfs 『大数据存储的进化史』
我们都知道现在大数据存储用的基本都是 Hadoop Hdfs ,但在 Hadoop 诞生之前,我们都是如何存储大量数据的呢?这次我们不聊技术架构什么的,而是从技术演化的角度来看看 Hadoop Hdf ...
- UDK脚本函数性能工具
数据采集 游戏中使用控制台命令来采集脚本函数性能数据 ProfileGame Start // 开始捕获性能数据 ProfileGame Stop // 停止捕获并保存数据文件,并保存到[Game ...