我使用sql server存储员工进进出出的午餐时间,并使用asp.net计算差额,如下所示。现在我想看看如果它们超过30分钟,那么值应该是红色的。
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Lunch Total Time
Label lunIn = (Label)e.Row.FindControl("lblLunIn");
Label lunOut = (Label)e.Row.FindControl("lblLunOut");
Label lunTotal = (Label)e.Row.FindControl("lblLunTotal");
lunTotal.Text = Convert.ToString(Convert.ToDateTime(lunOut.Text) - Convert.ToDateTime(lunIn.Text));
}我该怎么做呢?谢谢
发布于 2015-04-15 22:41:51
一般来说,我更喜欢使用真实的数据源,而不是GridViewRows中的文本。您可以通过e.Row.DataItem在RowDataBound中轻松获取。这样就可以防止本地化问题和对原始数据的处理。
但是,要从两个DateTime中获得以分钟为单位的时间,您可以将它们相减,然后使用返回的TimeSpan的TotalMinutes属性:
DateTime dtIn = Convert.ToDateTime(lunIn.Text);
DateTime dtOut = Convert.ToDateTime(lunOut.Text);
TimeSpan duration = dtOut - dtin;
lunTotal.Text = duration.ToString();
if(duration.TotalMinutes > 30)
{
e.Row.BackColor = Color.Red;
// or maybe you just want to change the color of the Label
lunTotal.ForeColor = Color.Red;
}https://stackoverflow.com/questions/29653067
复制相似问题