我有一个带有时间列的DataGrid,我需要求和以获得总时间,但我无法得到它! 我使用此代码尝试时间总和,使用Linq:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
textBox8.Text = dataGridView1.Rows.Cast<DataGridViewRow>()
.AsEnumerable()
.Sum(x => decimal.Parse(x.Cells["Duracao"].Value.ToString()))
.ToString();
}
有人可以帮我吗? 此致
答案 0 :(得分:2)
看看下面的代码示例。可能它会帮助你:
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; }
}