This is a migrated thread and some comments may be shown as answers.

Number of days (as integer) between two dates in Grid

2 Answers 429 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 08 Nov 2012, 09:57 PM
I have a RadGrid with two date columns and I have added a Calculated Column to get the number of days between them.

I tried a couple of techniques I found from Google to display the number of days but it always displays in this format:  2.00:00:00 for two days.  Fourty days looks like 40.00:00:00.  I just want to display the number of days as an integer like, "2" or "40".

The Calculated column UniqueName="DaysOpen".

One of the techniques I tried did the calculation in the .vb file and was supposed to send the value to a GridTemplateColumn with a <asp:Label /> in the ItemTemplate, but that method didn't work at all.  The column showed no values at all.  The column was there but all the fields were blank.
.aspx
        <telerik:GridCalculatedColumn DataFields="DateResolved,DateReceived" Expression="{0}-{1}" headerstyle-width="50px"
            HeaderText="Days Open" UniqueName="DaysOpen"  Aggregate="None">
        </telerik:GridCalculatedColumn>
 
 
<%--
        <telerik:GridTemplateColumn UniqueName="Temp" HeaderStyle-Width="50px" HeaderText="days open">
            <ItemTemplate>
              <asp:Label ID="DaysOpen" runat="server"></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
--%>


.aspx.vb  -  I commented out both methods because neither of them seemed to do anything.  In or out made no difference to the grid contents at all like they just weren't accessing the grid objects, which is probably the case.  On line 27 I hard coded a "3" in place of the now commented out, [date].ToString and the column was still blank.
Imports Telerik.Web.UI
Partial Class test
    Inherits System.Web.UI.Page
 
    'Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
    '    'grid.MasterTableView.SwapColumns("ComplaintID", "DateReceived")
    'End Sub
 
    'Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    '    If TypeOf e.Item Is GridDataItem Then
    '        Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
    '        Dim myTS As TimeSpan = TimeSpan.Parse(item("DaysOpen").Text)
    '        item("DaysOpen").Text = [String].Format("{0}h {1}m {2}s", myTS.Days, myTS.Hours, myTS.Minutes)
    '    End If
    'End Sub
 
    '  Both methods of getting the difference between two dates work equally well.
    '  Actually neither of them seem to do shit. When I commented them both out I still got the same correct results, but formatted like this:  2.00:00:00 for two days.
 
    'Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    '    If TypeOf e.Item Is GridDataItem Then
    '        Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
    '        Dim DaysOpen As Label = TryCast(item.FindControl("DaysOpen"), Label)
    '        Dim DateResolved As String = (item("DateResolved").Text)
    '        Dim DateReceived As String = (item("DateReceived").Text)
    '        Dim [date] As Integer = Convert.ToDateTime(DateResolved).Subtract(Convert.ToDateTime(DateReceived)).Days
    '        DaysOpen.Text = "3" ' [date].ToString()
    '    End If
    'End Sub
 
End Class

With my markup and code just as above the DaysOpen column displayes the correct number of days, just in the long format.  I just want to display it as an integer. e.g. "40" not "40.00:00:00"

Thank you!

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Nov 2012, 06:24 AM
Hello,

Please try with below code snippet.
<telerik:GridCalculatedColumn DataFields="DateResolved,DateReceived Expression="{0}-{1}"
                  DataFormatString="{0:dd}">


Thanks,
Jayesh Goyani
0
Meir
Top achievements
Rank 1
answered on 15 Sep 2016, 11:05 AM

this worked for me:

<sq:GridCalculatedColumn DataFields="fldToDate,fldFromDate" Expression="({0}-{1}).Value.Ticks/864000000000+1" HeaderText="Private Days" UniqueName="fldPrivateDays" DataType="System.Int32" Aggregate="Sum" ></sq:GridCalculatedColumn>

Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Meir
Top achievements
Rank 1
Share this question
or