Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I am trying to add a timer into a new column "Duration" that subtracts the time started, which is a date time, and displays how long it has been active. So far this is what I have:

XAML:

XML
<DataGridTextColumn Binding="{Binding BlockName}" Header="Block" Width="200"/>
    <DataGridTextColumn Binding="{Binding Auditor}" Header="Auditor" Width="200"/>
    <DataGridTextColumn Binding="{Binding AcctAuditor}" Header="Accounting Auditor" Width="200"/>
    <DataGridTextColumn Binding="{Binding BlockLeader}" Header="Block Leader" Width="200"/>
    <DataGridTextColumn x:Name="TimeStarted" Binding="{Binding TimeStarted}" Header="Started" Width="200"/>
    <DataGridTextColumn x:Name="Duration" Header="Elapsed Time" Width="200"/>


Code Behind:


C#
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
	foreach (DataRowView dr in BlockStatus.ItemsSource)
		{
		var diff = dr.Row.Field<DateTime>("TimeStarted").Subtract(DateTime.Now);
		if (diff.TotalSeconds > 0)
		{
		    dr.Row["Duration"] = string.Format("{0} d {1:D2}:{2:D2}:{3:D2}", diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
		}
		else
		{
		    dr.Row["Duration"] = "0 d 00:00:00";
        }
    }
}



I am trying to have a column that will count up in mm:ss in a new column based on another column.

Here is a picture of an example: https://prnt.sc/q2vrw3[^]

What I have tried:

I asked before, but I've reworded and hopefully I have explained it well enough
Posted
Updated 27-Nov-19 4:26am
v2
Comments
[no name] 26-Nov-19 12:41pm    
Here's your "timer": DateTime.Now - start time;
Knowledged 26-Nov-19 12:49pm    
but I would like it to be different for every row in the listview
johannesnestler 27-Nov-19 10:21am    
Yes, so what's your question or problem with that? (what Gerry showed was of course just for one timer (you have diffenet start-times, an Array or whatever - you didn't show any code). I think i understand what you want, I don't understand where you stuck...
Knowledged 27-Nov-19 10:26am    
updated whole question.
George Swan 27-Nov-19 10:15am    
Could I ask, what action stops the timers? Is it the button press of the button that started it?

1 solution

Google System.Reactive (also referred to as RX). It's a NuGEt package. I used it in a project that schedules sql jobs.

Google Ssstem.Reactive[^]

You can also look at an article I wrote here on CP - SQLXAgent - Jobs for SQL Express - Part 4 of 6[^]
 
Share this answer
 
v2

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