それぞれに型変換が必要です。
入力
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
class getSpecifiedData
{
public List<myModel> show()
{
List<myModel> returnList = new List<myModel>();
//ここで、Selectだと思うのですが、簡単に固定値を記述してます。
returnList.Add(new myModel("20170714", "9:00"));
returnList.Add(new myModel("20170714", "10:30"));
returnList.Add(new myModel("20170714", "15:30"));
return returnList;
}
}
public class myModel
{
private string _date;
private string _time;
public myModel(string id, string time)
{
this.date = id;
this.time = time;
}
public string date
{
get { return this._date; }
set { this._date = value; }
}
public string time
{
get { return this._time; }
set { this._time = value; }
}
}
}
実際に計算しているロジック
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
getSpecifiedData gsf = new getSpecifiedData();
BindingList<myModel> data = new BindingList<myModel>( gsf.show());
DateTime test = new DateTime();
foreach(myModel rows in data)
{
DateTime dt = DateTime.Parse(rows.time);//ここで文字列で取得した時間をDateTimeに変換
test += new TimeSpan(dt.Hour, dt.Minute, dt.Second);//変換したdtをTimeSpanにさらに変換してTestという引数に加算
}
MessageBox.Show(test.Day + "日と" + test.ToShortTimeString());//この場合は、2日と11:00という結果を出力しています。
}
}
}
追記(出力を時刻型にする)
C#
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10
11namespace WindowsFormsApplication1
12{
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private void Form1_Load(object sender, EventArgs e)
21 {
22 getSpecifiedData gsf = new getSpecifiedData();
23 BindingList<myModel> data = new BindingList<myModel>( gsf.show());
24 TimeSpan test = new TimeSpan();
25 foreach(myModel rows in data)
26 {
27 DateTime dt = DateTime.Parse(rows.time);//ここで文字列で取得した時間をDateTimeに変換
28 test += new TimeSpan(dt.Hour, dt.Minute, dt.Second);//変換したdtをTimeSpanにさらに変換してTestという引数に加算
29 }
30 //35:00という結果を出力しています。
31 MessageBox.Show(string.Format("{0}:{1}", ((int)test.TotalHours), string.Format("{0:D2}", test.Minutes)));
32 //データソースを設定する
33 dgv1.DataSource = data;
34 }
35 }
36}