C# 如何从gridview计算时间hh:mm

C# 如何从gridview计算时间hh:mm,c#,asp.net,C#,Asp.net,我曾经使用下面的代码对GridView中一列的总数求和,并将其用作分钟数图: int total = 0; protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { total += Convert.ToInt32(Data

我曾经使用下面的代码对GridView中一列的总数求和,并将其用作分钟数图:

int total = 0;
    protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Amount"));
        }
    }
列数据只是数字,例如:

2
33
50
120
01:30
00:45
00:05
02:00
但是现在为该列检索的数据是nvarchar(hh:mm),并希望将其相加,例如:

2
33
50
120
01:30
00:45
00:05
02:00
将是:

04:20
04:20

您可以使用TimeSpan.FromMinutes(minutesInDouble),以双精度格式传递上述值


您可以使用TimeSpan。从分钟(分钟)到双倍,以双精度格式传递上述值


您可以使用
System.TimeSpan
以时间格式获取结果

TimeSpan total = new TimeSpan();

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        var ts = DataBinder.Eval(e.Row.DataItem, "Amount").ToString(); 
        var t = DateTime.Parse(ts).TimeOfDay;
        totalTime += t;
    }

您可以使用
System.TimeSpan
以时间格式获取结果

TimeSpan total = new TimeSpan();

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        var ts = DataBinder.Eval(e.Row.DataItem, "Amount").ToString(); 
        var t = DateTime.Parse(ts).TimeOfDay;
        totalTime += t;
    }

您可以尝试以下方法:

static void Main(string[] args)
{
    string hours = @"
    01:30
    00:45
    00:05
    02:00";

    TimeSpan totalHoursSpan = hours.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
         .Select(x => DateTime.ParseExact(x.Trim(), "HH:mm", CultureInfo.InvariantCulture).TimeOfDay)
         .Aggregate(TimeSpan.Zero, (total, span) => total += span);


    string totalHrs = string.Format("{0:00}:{1:00}", (int)totalHoursSpan.TotalHours, totalHoursSpan.Minutes);

    Console.WriteLine(totalHrs);

}
输出:


您可以尝试以下方法:

static void Main(string[] args)
{
    string hours = @"
    01:30
    00:45
    00:05
    02:00";

    TimeSpan totalHoursSpan = hours.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
         .Select(x => DateTime.ParseExact(x.Trim(), "HH:mm", CultureInfo.InvariantCulture).TimeOfDay)
         .Aggregate(TimeSpan.Zero, (total, span) => total += span);


    string totalHrs = string.Format("{0:00}:{1:00}", (int)totalHoursSpan.TotalHours, totalHoursSpan.Minutes);

    Console.WriteLine(totalHrs);

}
输出:


你可以试着加上这些吗?如果默认情况下没有将数字解释为hh:mm,则可能需要为其提供一个格式提供程序。如果解析失败,您可能需要一些错误或警告突出显示。您可以尝试添加这些吗?如果默认情况下没有将数字解释为hh:mm,则可能需要为其提供一个格式提供程序。如果解析失败,您可能需要一些错误或警告突出显示。它工作正常,但某些输出需要一些解析,例如:00:24 00:08 00:20 00:05 15:15 16:20 00:05的结果是1.08:37:00,而它应该是32:37
var ts=DataBinder.Eval(e.Row.DataItem,TimeSpan.parse(“Amount”).ToString()此工作是否表示(无法从'System.TimeSpan'转换为'string')它工作,但某些输出需要一些解析,例如:00:24 00:08 00:20 00:05 15:15 16:20 00:05的结果是1.08:37:00,而它应该是32:37
var ts=DataBinder.Eval(例如Row.DataItem,TimeSpan.Parse(“Amount”)。ToString()这项工作是不是说(不能从'System.TimeSpan'转换为'string')它工作得很好,我必须为tsit添加.ToString()。它工作得很好,我必须为ts添加.ToString()