如何对DataGridView列中的时间求和 [英] How to sum Time from a DataGridView Columns
本文介绍了如何对DataGridView列中的时间求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有时间列的DataGrid,我需要对它进行求和以获得总时间,但我无法做到这一点!我正在用这段代码通过Linq尝试时间求和:
I have a DataGrid with a time column that I need to sum to get the total time, but I can't get that! I'm using this code to try the time sum, with Linq:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
textBox8.Text = dataGridView1.Rows.Cast<DataGridViewRow>()
.AsEnumerable()
.Sum(x => decimal.Parse(x.Cells["Duracao"].Value.ToString()))
.ToString();
}
有人可以在这里帮助我吗?问候
Can anybody help me here? Regards
推荐答案
看看下面的代码示例.可能会对您有帮助:
Have a look at below code sample. May be it will help you :
private void BindGrid()
{
// Bind Grid
var times = new List<Info>();
times.Add(new Info(){ Time = "0:3:5" });
times.Add(new Info() { Time = "0:2:10" });
times.Add(new Info() { Time = "1:15:30" });
this.dataGridView1.DataSource = times;
// Retrieve total seconds
double seconds = 0;
seconds = dataGridView1.Rows.Cast<DataGridViewRow>()
.AsEnumerable()
.Sum(x => TimeSpan.Parse((x.Cells["Time"].Value.ToString())).TotalSeconds);
// Assign to textbox
textBox8.Text = TimeSpan.FromSeconds(seconds).Hours + ":" + TimeSpan.FromSeconds(seconds).Minutes
+ TimeSpan.FromSeconds(seconds).Seconds;
}
信息类别:
public class Info
{
public string Time { get; set; }
}
这篇关于如何对DataGridView列中的时间求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文