Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir,

I have a gridview on my webpage

In the gridview, Textboxes are placed below every field,

My question is how to find time difference as by finding control in gridview

Like StartTime: 09:30<br />
<br />
      EndTime:  10:30


TotalTime Should come as by calculating difference between EndTime and StartTime

Like: EndTime-StartTime=> 10:30-09:30=1(TotalTime)

I have to do this by coding in C#

My problem is I am unable to findContol in gridview when calculating TotalTime

S.No.   StartTime   EndTime   TotalTime<br />
 1        09:30      10:30  



How Could I calculate Please guide me and provide some links so that I can solve my problem
Posted
Updated 18-Mar-16 1:56am
v2
Comments
Prashant Srivastava LKO 23-Dec-11 5:37am    
Thanks Uday
Prashant Srivastava LKO 23-Dec-11 5:38am    
Thanks For Always take care of mine

1 solution

C#
DateTime dFrom;
        DateTime dTo;
        string sDateFrom = "11:56:00";
        string sDateTo = "12:12:00";
        if (DateTime.TryParse(sDateFrom, out dFrom) && DateTime.TryParse(sDateTo, out dTo))
        {
            TimeSpan TS = dTo - dFrom;
            int hour = TS.Hours;
            int mins = TS.Minutes;
            int secs = TS.Seconds;
            string timeDiff = hour.ToString("00") + ":" + mins.ToString("00") + ":" + secs.ToString("00");
            Response.Write(timeDiff); //output 16 mins in format 00:16:00
        }
 
Share this answer
 
v2
Comments
Prashant Srivastava LKO 22-Dec-11 0:59am    
I need to do this in gridview How could I findControl in gridview and I am using Textboxes below these field in gridview in Item Template please help to this way
Prashant Srivastava LKO 23-Dec-11 5:37am    
Thanks Rakesh for this code I solved myself but by use of your code

My code is:
protected void txtStartTime_TextChanged1(object sender, EventArgs e)
{
filltime();
//int rowindex = grdDaily.SelectedIndex;
// TextBox txtStartTime = (TextBox)grdDaily.Rows[rowindex].FindControl("txtStartTime");
//TextBox txtEndTime = (TextBox)grdDaily.Rows[rowindex].FindControl("txtEndTime");
//TextBox txtTotalTime = (TextBox)grdDaily.Rows[rowindex].FindControl("txtTotalTime");
//int diff=TimeSpan
//txtTotalTime.Text=Convert.
}
public void filltime()
{
int rowindex = Convert.ToInt16(ViewState["RowIndex"]);
TextBox txtStartTime = (TextBox)grdDaily.Rows[rowindex].FindControl("txtStartTime");
TextBox txtEndTime = (TextBox)grdDaily.Rows[rowindex].FindControl("txtEndTime");
TextBox txtTotalTime = (TextBox)grdDaily.Rows[rowindex].FindControl("txtTotalTime");
DateTime ST;
DateTime ET;
string StartTime = txtStartTime.Text + ":00";
string EndTime = txtEndTime.Text + ":00";
if (DateTime.TryParse(StartTime, out ST) && DateTime.TryParse(EndTime, out ET))
{
TimeSpan TS = ET - ST;
int hour = TS.Hours;
//int mins = TS.Minutes;
// int secs = TS.Seconds;
string timeDiff = hour.ToString("00");// +":" + mins.ToString("00"); //+ ":" + secs.ToString("00");
//Response.Write(timeDiff); //output 16 mins in format 00:16:00 }
txtTotalTime.Text = timeDiff;
}
}
Arjunwalmiki 4-Jan-13 4:17am    
hello Prashant Srivastava LKO i am also have same query pls let me know all code if u resolve the problem

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