DisplayAttribute没作用,why?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | namespace WebBulletinBoard.DataAccess { using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; [Table( "BulletinBoardFile" )] public partial class BulletinBoardFile { [Key] public Guid FileKeyID { get ; set ; } [Display(Name = "展示日期" , Order = 0)] [StringLength(10)] public string WorkDate { get ; set ; } [Display(Name = "公告抬头" , Order = 1)] [StringLength(1024)] public string BulletinTitle { get ; set ; } [Display(Name = "公告描述" , Order = 2)] [StringLength(1024)] public string BulletinContexnt { get ; set ; } [Display(Name = "发布人" , Order = 4)] [StringLength(30)] public string UploadUser { get ; set ; } [Display(Name = "发布时间" , Order = 3)] public DateTime? UploadDateTime { get ; set ; } [Display(Name = "文件名" , Order = 5)] [StringLength(255)] public string FileName { get ; set ; } [Column(TypeName = "image" )] public byte [] BinaryData { get ; set ; } [Display(Name = "显示天数" , Order = 6)] public int DisplayDays { get ; set ; } public int ? OderIndex { get ; set ; } [Column(TypeName = "timestamp" )] [DatabaseGenerated(DatabaseGeneratedOption.Computed)] [MaxLength(8)] public byte [] RowVersion { get ; set ; } } } |
上面是实体类的定义。在给DataGridView绑定了上面的实体作为数据源之后,DisplayAttribute标记的中文列名没有显示出来。为什么呢?
实在没有办法,手动使用下面的办法解决了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | if (GridView1.Columns.Count > 0) { var props = typeof (BulletinBoardFile).GetProperties(); foreach ( var p in props) { var colParaObj = (DisplayAttribute)p.GetCustomAttributes( typeof (DisplayAttribute), false ).FirstOrDefault(); if (colParaObj != null ) { for ( int i = 0; i < GridView1.Columns.Count; i++) { if (GridView1.Columns[i].HeaderText.Equals(p.Name)) { GridView1.Columns[i].HeaderText = colParaObj.Name; break ; } } } } } |
如果凑巧看到这篇文章请赐教!
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/p/datacool20180417.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构