是否有可能将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.Now
和DataE
之间天数的差异?
我在哪里将这个新专栏放在代码上?
答案 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; } }
}