How to get the time-span of all the datetime data in my datagridview

﹥>﹥吖頭↗ 提交于 2019-12-04 07:02:27

问题


The scenario go's like that, above and this; I have a database, which I displayed on my datagridview. I already added the timespan of each row (timeIn and timeOut) and stored it on a textbox. Now, I wanted to add and display all the total timespans (which I already have the timespan of each rows timeIn and timeOut) of every row using only that textbox.

I guess my example is wrong, that should suppose to be all the emp_Code's has only '1' as its values in all three rows. Summary of the question: How can I get the timespan of row1 + timespan of row2 + timespan of row 3 and display it on a textbox. 'am thinking of using a loop, however I don't have any idea on how to do it. That's why am posting it here.

This is my front end, I don't have backend for this yet. When I press Compute, it'll display the total hours of all data in the datagridview(timespan of timeIn and timeOut) and breakdown the totalhours to Day shift and Night shift


回答1:


You have your time in and time out, so create a TimeSpan and run a loop:

TimeSpan timeSpan = new TimeSpan(0);
// for each data grid row calculate and add the timespan:
    timeSpan =+ time_Out.Subtract(time_In);
//



回答2:


According to the information you have provided

You need to iterate through every row of a Grid. You have to do something like this:

DateTime TotalTimeOfAllRows;

for (c= 0; c < DataGridView1.Rows.Count; c++)
{
    DateTime timeIn = DataGridView1.Rows[c].FindControl("txtTimeIn").Text;
  DateTime timeOut = DataGridView1.Rows[c].FindControl("txtTimeOut").Text;
  // Here Compute Datetime TotalTime
  DataGridView1.Rows[c].FindControl("txtTotalTime").Text = TotalTime.ToString();

  // Here Add TotalTime to TotalTimeOfAllRows     

}
//If TotalTimeOfAllRows Textbox is in Datagridview too
 DataGridView1.Rows[DataGridView1.Rows.Count-1].FindControl("txtTotalTimeOFAllRows").Text = TotalTimeOfAllRows.ToString();


来源:https://stackoverflow.com/questions/33075594/how-to-get-the-time-span-of-all-the-datetime-data-in-my-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!