Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys as iam new to .net ,iam finding this calculation is more difficult so plz help me to solve this.

iam having 2 columns empcode and their overtime in another column,i wanted to total the overtime column and display in a textbox field.eg,empcode as A111 and overtime as 05:30:00 and my code is


int tohours, tomin;
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//for (int i = 0; i < GridView2.Rows.Count; i++)
// {
foreach (GridViewRow row in GridView2.Rows)
{
// Label thrs = (Label)GridView2.Rows[i].FindControl("Overtime");
Label thrs = (Label)row.FindControl("Overtime");
TimeSpan ts = TimeSpan.Parse(thrs.Text);

tohours = ts.Hours;
tomin += ts.Minutes;


}
//}

}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label fthrs = (Label)e.Row.FindControl("lblTotalhrs");

fthrs.Text += (tohours + ":" + tomin).ToString();
}
}

Thanks in advance
Posted

Do this changes in your code and try.

tohours += ts.Hours;

fthrs.Text += ((tohours + (tomin / 60)) + ":" + (tomin % 60)).ToString();
 
Share this answer
 
C#
int tohours, tomin;
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label thrs = (Label)e.FindControl("Overtime");
            TimeSpan ts = TimeSpan.Parse(thrs.Text);

            tohours += ts.Hours;
            tomin += ts.Minutes;

        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label fthrs = (Label)e.Row.FindControl("lblTotalhrs");
            fthrs.Text = ((tohours + (tomin / 60)) + ":" + (tomin % 60)).ToString();

        }
    }
 
Share this answer
 
Comments
vijict 17-Jul-14 6:20am    
Thankyou so much.It worked perfect.
pradiprenushe 17-Jul-14 6:24am    
Welcome.Please accept solution. Its showing unsolved
pradiprenushe 17-Jul-14 6:30am    
you rejected answer. It worked or not?
vijict 17-Jul-14 6:38am    
I accepted the solution,it really worked.Thankyou.

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