3
votes

I have datagridview whose datasource is datatable.

I have two columns in datagridview:

NAME, IN TIME

After the datagridview is loaded, I want to add a new column called DURATION which is a timer column i.e. based on the IN TIME, DURATION column should display a stopwatch with the timespan interval of CURRENT TIME-IN TIME.

How do I make the DURATION column stopwatch to continue the timer for every seconds like digital clock?

1
How many rows did you have in your grid ? - Steve
@Steve For every thirty seconds, datagridview is updated. Row will not exceed more than twenty - CST RAIZE
Does it need to be decorated digital clock or a string hh:mm;ss that change every second should be ok? - Tuyen Pham
@Sakura Ya. thats what I need hh:mm:ss - CST RAIZE
That can be done use Timer and delegate event. Could you post your code about C# data struct and how you bind it to DGV? - Tuyen Pham

1 Answers

2
votes

You can add a string column to DataTable and use it as duration column. Then you can subscribe to Tick event of a timer which you set it's interval to 1000 and show the result of count down timer in duration column:

void timer_Tick(object sender, EventArgs e)
{
    foreach( DataRow row in YourDataTable.Rows)
    {
        var diff = row.Field<DateTime>("DateTimeColumn").Subtract(DateTime.Now);
        if(diff.TotalSeconds>0)
        {
            row["DurationColumn"] = string.Format("{0} d {1:D2}:{2:D2}:{3:D2}", 
                                       diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
        }
        else
        {
            row["DurationColumn"] = "0 d 00:00:00";
        }
    }
}