Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
XML
<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>


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


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?
Posted

1 solution

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.
 
Share this answer
 
Comments
Hayashi Narumi 16-Aug-15 9:43am    
I'm not sure how to bind that additional calculated property in the GridView... can you give me an example?
kbrandwijk 16-Aug-15 9:52am    
As I guess you are using a DataReader to fill a DataTable, add a column to your DataTable with an Expression, see http://www.codeproject.com/Articles/447249/Calculated-Columns-in-NET-DataTables-Csharp for an example. Then bind that column as any other column.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900