zoukankan      html  css  js  c++  java
  • DisplayAttribute没作用,why?

    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标记的中文列名没有显示出来。为什么呢?

    实在没有办法,手动使用下面的办法解决了。

    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;
                                    }
                                }
                            }
                        }
                    }
    

     如果凑巧看到这篇文章请赐教!

  • 相关阅读:
    新家开始了
    FreeMarker过时的方法 new Configuration()
    基于Spring封装的Javamail实现邮件发送
    Spring中PropertiesLoaderUtils应用
    SpringBoot基于数据库的定时任务统一管理
    ActiveMQ点对点模式
    springboot整合swagger2
    dubbo简单示例
    SpringCLoud之搭建Zuul网关集群
    微服务项目框架搭建
  • 原文地址:https://www.cnblogs.com/datacool/p/datacool20180417.html
Copyright © 2011-2022 走看看