如何在GridView中显示DateTime MySql的减法 [英] How to display subtraction of DateTime MySql in GridView

查看:90
本文介绍了如何在GridView中显示DateTime MySql的减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将过期日期存储在MySQL数据库中,因此我只想在GridView中显示减去Now()日期时间的日期。



我'已经看到了很多关于DateTime减法的答案,但问题是,它们都使用Behind Code(C#)。我不知道在编码后怎么做。



这就是我对GridView页面所做的...

I've stored the expiry date in MySQL DB and so I just wanted to display the date that's subtracted to Now() datetime in GridView.

I've seen a lot of answers regarding the subtraction of DateTime but the problem is, they all use Behind Code (C#). I don't know what to do after coding behind.

This is what I did to the GridView page...

<asp:TemplateField HeaderStyle-CssClass=" panel-heading text-center" HeaderText="DUE">
                    <ItemTemplate>
                        <asp:Label ID="countdown" CssClass="col-sm-10 text-center" runat="server" Text='<%# String.Format("{0} days left", Eval("countdown", "{0: dd/MM/yyyy}")) %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>





有没有办法在GridView中显示减去的DateTime值?

我的想法是这样的...



Is there any way to display the subtracted DateTime value in GridView?
My idea is like this...

        //get countdown from db               
                        DateTime countdown_new = new DateTime();
countdown_new = dataReader.GetDateTime(dataReader.GetOrdinal("countdown")); 
                        DateTime now = DateTime.Now;
                        int countdown = (int)(countdown_new - now).TotalDays;

//to be displayed in GridView
 string display = "Time left:" + countdown.Day + "Days";





我不知道还能做什么...

所以如果可能的话,有没有办法在GridView标签中使用String.Format文本?



I don't know what else to do...
So if possible, is there any way to just use the String.Format Text in GridView Label?

推荐答案

你可以,但你不应该。您的视图不是开始进行日期计算的最佳位置。如果您对MVC或MVVM等设计模式有任何经验,您会知道这是放在ViewModel(或等效的)中的东西。只需创建一个额外的计算属性,并将其绑定在View中,就像所有其他属性一样。
You could, but you shouldn't. Your View is not the best place to start doing date calculations. If you have any experience with design patterns like MVC or MVVM, you would know that this is the kind of stuff to put in your ViewModel (or equivalent). Just create an additional calculated property, and bind it in your View like all other properties.


这篇关于如何在GridView中显示DateTime MySql的减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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