Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to subtract two times like hh:mm:ss in this format in datagrid cells ,and the subtracted value will be display in next cell of the datagrid in c# windows application
Posted

I wouldn't advice to do this inside the data grid. In my opinion the classes containing the data should be responsible of providing the calculated values. The data grid would only show them.

For example if you're using a DataTable, I would create a new, calculated column into the data table and the expression would be the substraction thus providing the calculated value as a normal data column. The data from this column would be bound to the data grid just like a normal column, only read-only.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Aug-15 12:36pm    
Good point, a 5. See my 5 cents in Solution 2.
—SA
In addition to Solution 1: I hope you are using not with strings representing date/time, but with the type System.DateTime. It defines the '-' subtraction operator which should be used to find out the time difference; the type of result is System.TimeSpan. Using the operator is much clearer than calling the Subtract function:
https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx[^].

Nowhere in intermediate results should you use strings representing time or duration, but the type of the cell in the grid view could be string, if you only use it for on-screen output.

—SA
 
Share this answer
 
Comments
Wendelius 15-Aug-15 2:22am    
Good point!
Sergey Alexandrovich Kryukov 15-Aug-15 11:20am    
Thank you, Mika.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900