How do I display a digital timer (StopWatch) in datagridview column dynamically c#?

本秂侑毒 提交于 2019-12-02 00:26:34

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";
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!