I have a Gridview in which I am pulling data from an access database. One of the columns in the gridview pulls a field named Last Login Date. I want to calculate the difference between the Last Login Date and the Current Date and display the calculated difference in an additional column in the same row. I have verified that the calculation is happening just fine. But when the page is displayed, the calculated value is getting displayed in the next row instead of the same row. The value of Difference in the first row is showing as 0 (zero).
Calculated Values | |||
Idle Since | Current Date and Time | Last_Log_Date | |
Showing 0 instead of 9. 9 is getting displayed in next row.--------------------------------------- | 0 | 7/26/2008 11:23 | 7/17/2008 22:46 |
This value is actually the calculated value using the data fields in previous row----- | 9 | 7/26/2008 11:23 | 7/17/2008 22:54 |
same as above | 9 | 7/26/2008 11:23 | 7/25/2008 2:32 |
' | 1 | 7/26/2008 11:23 | 7/23/2008 9:49 |
' | 3 | 7/26/2008 11:23 | 7/23/2008 5:23 |
' | 3 | 7/26/2008 11:23 | 7/23/2008 5:24 |
' | 3 | 7/26/2008 11:23 | 7/17/2008 18:35 |
Here's the code behind my ASPX Page:
Imports
System.ComponentModelPartial Class _Default Inherits System.Web.UI.Page Public idleage As IntegerProtected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If (e.Row.RowType = DataControlRowType.DataRow) Then 'Add Decoration in the Gridview when a row is clickede.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';this.style.textDecoration='underline';")e.Row.Attributes.Add(
"onmouseout", "this.style.textDecoration='none';")e.Row.Attributes.Add("onclick", "this.style.cursor='hand';this.style.forecolor='blue';")e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" + e.Row.RowIndex.ToString()))
'Calculate difference between Last Login Date and Current DateDim lastlog As String = e.Row.Cells(2).Text Dim dt As Date = DirectCast((TypeDescriptor.GetConverter(New Date(2008, 7, 1)).ConvertFrom(lastlog)), Date)Dim diff As TimeSpan = Date.Now.Subtract(dt)
idleage = diff.TotalDays
End IfEnd SubEnd
ClassThe code on the aspx page is below.
<
html xmlns="http://www.w3.org/1999/xhtml"><
head runat="server"><title>Untitled Page</title></
head><
body> <form id="form1" runat="server"><asp:GridView ID="GridView1" runat="server" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> <Columns> <asp:templatefield headertext="Idle Since" itemstyle-horizontalalign="Right" footerstyle-horizontalalign="Right" footerstyle-backcolor="Blue" footerstyle-forecolor="White"> <itemtemplate> <asp:label id="Idle" runat="server" Text='<%#idleage%>'/> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="Current Date and Time" itemstyle-horizontalalign="Right" footerstyle-horizontalalign="Right" footerstyle-backcolor="Blue" footerstyle-forecolor="White"> <itemtemplate> <asp:label id="Today" runat="server" Text='<%#DateTime.Now%>'/> </itemtemplate> </asp:templatefield> </Columns> <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:New_TestConnectionString %>" ProviderName="<%$ ConnectionStrings:New_TestConnectionString.ProviderName %>" SelectCommand="SELECT [Last Log Date] AS Last_Log_Date FROM [Linked_Save]"> </asp:SqlDataSource></form></
body></
html>how can i correct this problem. Appreciate the help.
Thanks,
Prateek
![]() |
0 |
![]() |
Hi lost_floyd,
I have done a few modifications on your aspx and aspx.cs page
Coding is on C#
Simple enough to convert
In ASPX
<asp:GridView ID="GridView1" runat="server" CellPadding="4" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"
ForeColor="#333333" GridLines="None"
onrowdatabound="GridView1_RowDataBound">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Idle Since" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right"
FooterStyle-BackColor="Blue" FooterStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblIdle" runat="server" Text='<%#idleage%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Current Date and Time" ItemStyle-HorizontalAlign="Right"
FooterStyle-HorizontalAlign="Right" FooterStyle-BackColor="Blue" FooterStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblToday" runat="server" Text='<%#DateTime.Now%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Login" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right"
FooterStyle-BackColor="Blue" FooterStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblDBDate" runat="server" Text='<%#Eval("Last_Log_Date")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
In ASPX.CS
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblDateNow = e.Row.FindControl("lblToday") as Label;
Label lblDateDB = e.Row.FindControl("lblDBDate") as Label;
Label lblIdleSince = e.Row.FindControl("lblIdle") as Label;
DateTime dtToday = Convert.ToDateTime(lblDateNow.Text);
DateTime dtDB = Convert.ToDateTime(lblDateDB.Text);
TimeSpan tsDateDiff = dtToday - dtDB;
lblIdleSince.Text = tsDateDiff.Days.ToString();
}
}
This will do.
Regards,
Naveen
Please remember to click Mark as Answer on the post that helps you, and to click Unmark as Answer if a marked post does not actually answer your question.
View Blog
![]() |
0 |
![]() |
Thanks a Ton Naveen... that solved my problem... you guys are wonderful !!
Cheers !
![]() |
0 |
![]() |