我将根据今天任务的天数更改gridview的行颜色。但它没有用 date1是今天的日期 和date2是任务的截止日期。
当我点击列标题进行排序时,行会改变颜色
Protected Sub GridView6_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Dim date1 As Date
date1 = Date.Now
Dim date2 As Date
For Each row As GridViewRow In GridView6.Rows
Dim ddate As Label = CType(row.FindControl("label1"), Label)
date2 = Date.Parse(ddate.Text)
Dim ts As TimeSpan = date2.Subtract(date1)
Dim days As Integer = ts.TotalDays
If days <= 14 Then
e.Row.ForeColor = System.Drawing.Color.Red
ElseIf days > 14 And ts.Days < 30 Then
e.Row.ForeColor = System.Drawing.Color.Blue
ElseIf days >= 30 Then
e.Row.ForeColor = System.Drawing.Color.LightGreen
End If
Next
End Sub
答案 0 :(得分:2)
花了一段时间,但最终我发现了它。
你循环遍历每一行,然后只更新一个数据绑定!
摆脱grid.Rows中的foreach行,只需处理e.Row中的行。
您的代码应为:
Protected Sub GridView6_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Dim date1 As Date
date1 = Date.Now
Dim date2 As Date
Dim ddate As Label = CType(e.Row.FindControl("label1"), Label)
date2 = Date.Parse(ddate.Text)
Dim ts As TimeSpan = date2.Subtract(date1)
Dim days As Integer = ts.TotalDays
If days <= 14 Then
e.Row.ForeColor = System.Drawing.Color.Red
ElseIf days > 14 And ts.Days < 30 Then
e.Row.ForeColor = System.Drawing.Color.Blue
ElseIf days >= 30 Then
e.Row.ForeColor = System.Drawing.Color.LightGreen
End If
End Sub
答案 1 :(得分:0)
您可能需要TotalDays
中的TimeSpan
。
答案 2 :(得分:0)
替换
date1 = Date.Now
与
date1 = Date.Now.Date
Date.Now也包含时间部分。从上下文来看,您只对日期差异感兴趣,而不考虑时间。