-1

SAMPLE DATABASE DESIGN

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.

enter image description 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

pruuylan
  • 86
  • 1
  • 1
  • 10
  • I don't think I understand your question clearly. If you store them in `datetime`, you can get these values as `DateTime` and use `TimeOfDay` property to get? Can you please be more specific? – Soner Gönül Oct 12 '15 at 07:44
  • @SonerGönül oooopppsss. I guess my example is wrong, lets say that all the employee codes are all 1 with the same data in it. Now, I wanted to get the total timespan(which is row1+row2+row3's timespans) and display it in one textbox – pruuylan Oct 12 '15 at 07:50
  • @Fabjan I had done computing the timespan of each rows timein and timeout, only. – pruuylan Oct 12 '15 at 07:51
  • @SuperPruuylan Your values seems `DateTime` rathen than a `TimeSpan`. You can iterate your values _row by row_ and get this values as a `DateTime` and use `TimeOfDay` property to get their time parts. – Soner Gönül Oct 12 '15 at 07:52
  • @SonerGönül please see the edited question sir, I guess it's more clear. :) looking forward for your reply, sir. – pruuylan Oct 12 '15 at 07:56

2 Answers2

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);
//
Gustav
  • 53,498
  • 7
  • 29
  • 55
  • Yes. Go ahead and try. I just indicated the loop as I don't have your code. – Gustav Oct 12 '15 at 08:56
  • I already tried it, but am having trouble on getting timeIn and timeOut since it is on datagridview. Here is the code sir, 'DateTime time_Out = Convert.ToDateTime(datagridAttendance.CurrentRow.Cells["timeIn"]);' 'DateTime time_In = Convert.ToDateTime(datagridAttendance.CurrentRow.Cells["timeOut"]);' – pruuylan Oct 12 '15 at 08:58
  • I am having trouble on how to increment the index – pruuylan Oct 12 '15 at 09:00
0

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();
Waqar Ahmed
  • 1,414
  • 2
  • 15
  • 35