使用动态计算将列添加到DataGridView

时间:2018-06-07 08:37:20

标签: c# datagridview

是否有可能将DataGridView的列添加到动态计算值?

我有一个DataGridView填充LINQ查询(ToList)。我正在使用EF6(数据库优先)。

我需要在查询集合中添加一个未绑定的列,以计算两个日期之间的天数。

这是我的DataGridView填充列表:

public class Recl
    {
        public int ID { get; set; }
        public string Concelho { get; set; }
        public string Entidade { get; set; }
        public string LC { get; set; }
        public string Classificacao { get; set; }
        public string TipoR { get; set; }
        public string TipoE { get; set; }
        public string Identificador { get; set; }
        public string Estado { get; set; }
        public decimal? Custos { get; set; }
        public string Descricao { get; set; }
        public DateTime DataR { get; set; }
        public DateTime DataE { get; set; }
        public DateTime? DataRespostaEE { get; set; }
        public DateTime? DataRespostaC { get; set; }
    }

    public IEnumerable<Recl> LoadData()
    {
        var ctx = new IWMJEntities();
        var fillrecl = (from p in ctx.tblRecl.AsQueryable()
                         join c in ctx.tblConc on p.ConcID equals c.ConcID
                         join e in ctx.tblEnt on p.EntID equals e.EntID
                         join ex in ctx.tblLC on p.LCID equals ex.LCID
                         join cl in ctx.tblClassificacao on p.ClassificacaoID equals cl.ClassificacaoID
                        join tp in ctx.tblTipoR on p.TipoRID equals tp.TipoRID
                        join te in ctx.tblTipoE on p.TipoEID equals te.TipoEID
                        select new Recl
                        {
                             ID = p.ReclID,
                             Concelho = c.NomeConcelho,
                             Entidade = e.Nome,
                             LC = ex.Codigo,
                             Classificacao = cl.Classificacao,
                             TipoR = tp.TipoR,
                             TipoE = te.TipoE,
                             Identificador = p.Identificador,
                             Estado = p.Estado,
                             Custos = p.Custos,
                             DataR = p.DataR,
                             DataE = p.DataE,
                             DataRespostaEE = p.DataRespostaEmpresa,
                             DataRespostaCliente = p.DataRespostaCliente
                         }).ToList();
        return fillrecl;
    }

从Load Event加载DataGridView的调用:

datagrid.DataSource = LoadData();

如何添加一个列(天)来计算DateTime.NowDataE之间天数的差异?

我在哪里将这个新专栏放在代码上?

2 个答案:

答案 0 :(得分:0)

将属性(比如DifferenceInDays)添加到Recl类,并在select子句中添加DifferenceInDays = (DateTime.Now - p.DataE).Days

答案 1 :(得分:0)

只需在您的班级中添加一个属性:

public class Recl
{
    ...    
    public DateTime DataE { get; set; }

    public TimeSpan DaysDifference { get { return DateTime.Now - DataE; } }
}