0

我在gridview的列(日期时间)中有一些值(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);
     }

但它不起作用!

4

3 回答 3

2

你可以搭配 -hours and hoursCpt

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

所以

int hours, minutes, hoursCpt, minutesCpt;
hoursCpt = 0;
minutesCpt = 0;

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;

    hoursCpt += hours;
    minutesCpt  += minutes;
 }
 footxt.Text = Convert.ToString(hoursCpt + ((minutesCpt  - minutesCpt  % 60) / 60));
于 2012-10-12T15:53:46.973 回答
1

现在,您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();
于 2012-10-12T15:55:54.017 回答
1

使用 TimeSpan 的operator +

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);
于 2012-10-12T16:00:34.367 回答