计算位于DataGrid不同行中的列值之间的日期差异? [英] Calculate Date Difference between column values located in different rows of a DataGrid?

查看:76
本文介绍了计算位于DataGrid不同行中的列值之间的日期差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 绑定到DataGrid一个ObservableCollection,是有可能有一个显示行和所述一个&NBSP之间的时间差(比如说在个月)计算列;上述(之前)它,对于所有的行在DataGrid中? 


公共类EventData 
{
public int ID {get; set;}
public DateTime EventDate {get; set;}
}


 private ObservableCollection< EventData>事件; 
public ObservableCollection< EventData>活动
{
get {return events; }
}

事件=新的ObservableCollection< EVENTDATA>()
{
新的事件(){ID = 1,EVENTDATE = '2017年9月21日' };
new events(){ID = 2,EventDate ='2017-11-15'};
new events(){ID = 3,EventDate ='2018-02-07'};
new events(){ID = 4,EventDate ='2018-04-25'};
新事件(){ID = 5,EventDate ='2018-06-03'};
}



我想要DataGrid数据 显示每行和之前的日期差异。第一行将显示0(或空),因为没有要比较它。


<预类= "prettyprint"> ID EVENTDATE LastEvent

1 2017-09 -21 0
2 2017-11-15 2
3 2018-02-07 3
4 2018-04-25 2
5 2018-06-03 2

我怎样才能完成这样的事情?






解决方案

您需要使用MVVM模式。 如果你做了你的情况下的模型(数据)是EventData。 您要绑定到datagrid的ItemsSource的是ViewModel,它将包含您的EventData和应用程序中所需的任何其他属性。 
在您的情况下,您希望在ViewModel的属性获取器中设置日期差异。


With an ObservableCollection bound to a DataGrid, is it possible to have a calculated column that displays the date difference (say in months) between a row and the one above it (before it), for all rows in the DataGrid? 

public class EventData
{
  public int ID {get; set;}
  public DateTime EventDate {get; set;}
}

private ObservableCollection<EventData> events;
public ObservableCollection<EventData> Events
{ 
  get{ return events; }
}

events = new ObservableCollection<EventData>()
{
    new events(){ID = 1, EventDate = '2017-09-21'};
    new events(){ID = 2, EventDate = '2017-11-15'};
    new events(){ID = 3, EventDate = '2018-02-07'};
    new events(){ID = 4, EventDate = '2018-04-25'};
    new events(){ID = 5, EventDate = '2018-06-03'};
}



I would like the DataGrid data to display the date difference between each row and the one before it like this. The first row would display 0 (or null) because there is nothing to compare it to.

ID  EventDate  LastEvent  

1   2017-09-21   0
2   2017-11-15   2
3   2018-02-07   3
4   2018-04-25   2
5   2018-06-03   2

How might I accomplish something like this?



解决方案

You need to use the MVVM pattern.  If you do the Model (data) in your case is EventData.  What you want to bind to the ItemsSource of the datagrid is a ViewModel which would contain both your EventData and any other properties needed in the app.  In your case you want to have a date difference which you can set up in the property getter of the ViewModel.


这篇关于计算位于DataGrid不同行中的列值之间的日期差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆