views:

1002

answers:

2

I'm struggling a bit with a calculation I need to do en every row in a datagridview. What I want is to go through all rows in a datagridview that has two columns with datetimes. One representing a LogInTime and one representing a LogOutTime. I would like to get the total number of hours and minutes between all log in times and log out times for the rows displayed in the datagridview. Any suggestions on how to implement this?

+1  A: 

What's your DataGridView bound to? A calculation like that would be easier to do on the code behind using a Linq expression or a DataTable.Compute() call.

An example Linq expression:

var minutes = listOfLogins.Sum(l => l.LogOutTime.Subtract(l.LoginTime).TotalMinutes);
Console.WriteLine("{0}h, {1}m", (int)(minutes / 60), minutes % 60);
Matt Hamilton
It is bound to a DataTable. Linq is unfortunately not an option here due to the project specification. I think you have a good suggestion there and I'll try to modify our code and see how it works out. Thanks!
daft
Cool. Worst case scenario you could loop through the DataTable, but it might be possible to add up the DateTime differences in a single Compute("SUM(...)") method call. I don't think datediff() is supported but it's worth a try.
Matt Hamilton
+1  A: 

You can subtract one date from another to get a TimeSpan unit which will give you the data you require.

You will need to calculate this in your source data, or in the row data bound event you can get the data and do the calculation.

ck