C# 如何在gridview的列中添加不同的时间并在页脚文本框中显示总和

C# 如何在gridview的列中添加不同的时间并在页脚文本框中显示总和,c#,asp.net,C#,Asp.net,我在gridview的一列(datetime)中有一些值(08:12,09:22,01:09) 我想要的是在该列的页脚中的文本框中获取该列中所有值的总和 我尝试了以下代码- int hours, minutes; TextBox footxt = (TextBox)GridView2.FooterRow.FindControl("footxt1"); foreach (GridViewRow row in GridView2.Rows) { Tex

我在gridview的一列(datetime)中有一些值(08:12,09:22,01:09)
我想要的是在该列的页脚中的文本框中获取该列中所有值的总和

我尝试了以下代码-

    int hours, minutes;
    TextBox footxt = (TextBox)GridView2.FooterRow.FindControl("footxt1");
    foreach (GridViewRow row in GridView2.Rows)
    {
        TextBox txts = (TextBox)row.FindControl("text11");

        TimeSpan ts = TimeSpan.Parse(txts.Text);
        hours = ts.Hours;
         minutes = ts.Minutes;
        hours += hours;
        minutes += minutes;
     footxt.Text = Convert.ToString(hours);
     }

但是它不起作用

您可以使用-
小时数CPT进行训练

    ... 
    hours = ts.Hours;
    minutes = ts.Minutes;
    hoursCpt += hours;
所以


您可以使用-
hoursCpt

    ... 
    hours = ts.Hours;
    minutes = ts.Minutes;
    hoursCpt += hours;
所以


现在,每次循环都会覆盖
hours
,因此您将添加到它,但当返回到下一个值时,会将其删除

int totHours, totMinutes
TextBox footTxt = (TextBox)GridView2.FooterRow.FindControl("footxt1");

foreach (GridViewRow row in GridView2.Rows)
{
    TextBox txts = (TextBox)row.FindControl("text11");
    TimeSpan ts = TimeSpan.Parse(txts.Text);

    totHours += ts.Hours;
    totMinutes += ts.Minutes;

}
footTxt.Text = totHours.ToString();

现在,每次循环都会覆盖
hours
,因此您将添加到它,但当返回到下一个值时,会将其删除

int totHours, totMinutes
TextBox footTxt = (TextBox)GridView2.FooterRow.FindControl("footxt1");

foreach (GridViewRow row in GridView2.Rows)
{
    TextBox txts = (TextBox)row.FindControl("text11");
    TimeSpan ts = TimeSpan.Parse(txts.Text);

    totHours += ts.Hours;
    totMinutes += ts.Minutes;

}
footTxt.Text = totHours.ToString();

使用TimeSpan的
运算符+

TimeSpan total = new TimeSpan();
TextBox footxt = (TextBox)GridView2.FooterRow.FindControl("footxt1");
foreach (GridViewRow row in GridView2.Rows)
{
    TextBox txts = (TextBox)row.FindControl("text11");
    total += TimeSpan.Parse(txts.Text);
}
footxt.Text = Convert.ToString(hours);

使用TimeSpan的
运算符+

TimeSpan total = new TimeSpan();
TextBox footxt = (TextBox)GridView2.FooterRow.FindControl("footxt1");
foreach (GridViewRow row in GridView2.Rows)
{
    TextBox txts = (TextBox)row.FindControl("text11");
    total += TimeSpan.Parse(txts.Text);
}
footxt.Text = Convert.ToString(hours);

它不起作用-怎么办?你能解释清楚吗?它不起作用-怎么起作用?你能解释清楚吗?+1表示正确处理溢出。。。但是TimeSpan+要容易得多:)我试过了,但它在hoursCpt上给了我一个错误,说是未分配的局部变量…我在foreach循环+1之前取了int hoursCpt来正确处理溢出。。。但是TimeSpan+要容易得多:)我试过了,但它在hoursCpt上给了我一个错误,说的是未赋值的局部变量…我在foreach之前已经使用了int hoursCptloop@MichaelCode,考虑一些有用的答案,并接受回答你的问题的答案——如果你想问更多的问题,而没有被接受的答案的问题很多,就会降低获得好答案的机会,所以早点开始:“MichaelCode,考虑一些有用的答案,接受回答你的问题的答案——如果你想问更多的问题,而没有被接受的答案,那么就有机会得到很好的答案,所以早点开始: