分享Winform datagridview 动态生成中文HeaderText
2018-07-20 19:23
639 查看
缘起
很久以前给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(); } }
相关文章推荐
- 版本4.0aimkind字典中显示中文含义和类型名称,并过滤生成aimcode字典。构造动态字典。
- DataColumn.Caption属性应用到DataGridView.HeaderText的方法
- winform 动态生成panel 动态生成透明panel 带边框的panel 并注册事件
- 如何动态改变 DataGridView 的选择行(WinForm)
- iText编辑PDF动态生成文档,支持中文
- 关于js中文乱码问题和js事件代理动态生成div
- C# WinForm TreeView 动态生成 无限级树 Log4net SQLite
- 动态改变GridView的HeaderText的值
- 分享一个动态生成RDLC报表的类 - thanks
- 动态生成table并实现分页效果心得分享
- java随机动态生成汉字验证码图片的实例代码分享
- Java iText 动态 生成 PDF 文档表格中文问题
- Android布局中动态修改TextView等控件中文字的颜色
- vb.net winform动态生成菜单(源码)
- winform中在一个DataGridView中实现多个表的动态切换
- Asp.net动态生成html页面的方法分享
- 分享动态生成文字图片解决方案
- winform,DataGridView绑定数据后更改列属性、动态创建(用一个控件显示多表适用)
- 【分享】Winform如何动态的保存配置
- ssm项目使用Mybatis动态拼接sql语句,生成的sql中文全部显示为???的问题(配置文件sql语句中文解析问题)