如何根据日期值对Gridview进行颜色编码

时间:2019-05-31 07:46:38

标签: asp.net vb.net

我想让我的Gridview控件根据条件显示各种颜色。一个人有一个约会的日期。我希望当一天到来时gridview显示绿色。我希望日期从到期日起过去一天时显示为红色,而在医疗日期前7天显示为橙色。我只设法将其修复为可显示当天医疗状况的绿色,但在其他情况下我遇到了困难。

这是我目前拥有的。当我根据到期日和当前日期相等将行更改为绿色时,If语句的第一部分起作用,但是我不知道如何为上述其他条件设置if语句。请协助

 If e.Row.RowType = DataControlRowType.DataRow Then


            Dim row As DataRow = (CType(e.Row.DataItem, DataRowView)).Row
            Dim DueDate As DateTime = row.Field(Of DateTime)(7)

            If DueDate.Date = DateTime.Today Then

                e.Row.Cells(0).BackColor = System.Drawing.Color.Green
                e.Row.Cells(0).ForeColor = System.Drawing.Color.White

                e.Row.Cells(1).BackColor = System.Drawing.Color.Green
                e.Row.Cells(1).ForeColor = System.Drawing.Color.White

                e.Row.Cells(2).BackColor = System.Drawing.Color.Green
                e.Row.Cells(2).ForeColor = System.Drawing.Color.White

                e.Row.Cells(3).BackColor = System.Drawing.Color.Green
                e.Row.Cells(3).ForeColor = System.Drawing.Color.White

                e.Row.Cells(4).BackColor = System.Drawing.Color.Green
                e.Row.Cells(4).ForeColor = System.Drawing.Color.White

                e.Row.Cells(5).BackColor = System.Drawing.Color.Green
                e.Row.Cells(5).ForeColor = System.Drawing.Color.White

                e.Row.Cells(6).BackColor = System.Drawing.Color.Green
                e.Row.Cells(6).ForeColor = System.Drawing.Color.White

                e.Row.Cells(7).BackColor = System.Drawing.Color.Green
                e.Row.Cells(7).ForeColor = System.Drawing.Color.White

                e.Row.Cells(8).BackColor = System.Drawing.Color.Green
                e.Row.Cells(8).ForeColor = System.Drawing.Color.White

                e.Row.Cells(9).BackColor = System.Drawing.Color.Green
                e.Row.Cells(9).ForeColor = System.Drawing.Color.White

                e.Row.Cells(10).BackColor = System.Drawing.Color.Green
                e.Row.Cells(10).ForeColor = System.Drawing.Color.White

                e.Row.Cells(11).BackColor = System.Drawing.Color.Green
                e.Row.Cells(11).ForeColor = System.Drawing.Color.White

                e.Row.Cells(12).BackColor = System.Drawing.Color.Green
                e.Row.Cells(12).ForeColor = System.Drawing.Color.White

                e.Row.Cells(13).BackColor = System.Drawing.Color.Green
                e.Row.Cells(13).ForeColor = System.Drawing.Color.White


            ElseIf Day(DateTime.Now) > 1 Then


                e.Row.Cells(0).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(0).ForeColor = System.Drawing.Color.White

                e.Row.Cells(1).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(1).ForeColor = System.Drawing.Color.White

                e.Row.Cells(2).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(2).ForeColor = System.Drawing.Color.White

                e.Row.Cells(3).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(3).ForeColor = System.Drawing.Color.White

                e.Row.Cells(4).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(4).ForeColor = System.Drawing.Color.White

                e.Row.Cells(5).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(5).ForeColor = System.Drawing.Color.White

                e.Row.Cells(6).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(6).ForeColor = System.Drawing.Color.White

                e.Row.Cells(7).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(7).ForeColor = System.Drawing.Color.White

                e.Row.Cells(8).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(8).ForeColor = System.Drawing.Color.White

                e.Row.Cells(9).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(9).ForeColor = System.Drawing.Color.White

                e.Row.Cells(10).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(10).ForeColor = System.Drawing.Color.White

                e.Row.Cells(11).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(11).ForeColor = System.Drawing.Color.White

                e.Row.Cells(12).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(12).ForeColor = System.Drawing.Color.White

                e.Row.Cells(13).BackColor = System.Drawing.Color.Orange
                e.Row.Cells(13).ForeColor = System.Drawing.Color.White

            End If

        End If

3 个答案:

答案 0 :(得分:0)

您可以按如下所示使用方法后面的代码:

像这样在GridView中添加模板列:

<asp:TemplateField HeaderText="Due Date">
    <ItemTemplate>
        <asp:Label ID="Label4" runat="server" Text='<%# Eval("DueDate") %>'
            Style='<%# GetColorStyles(0, Eval("DueDate"))%>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

,并对每个单元格使用以下方法:

Function GetColorStyles(ByVal cellindex As String, ByVal DueDate As DateTime) As String
    Dim RES As String = "color:#000000" 'default color
    If DueDate.Date = DateTime.Today Then
        RES = "color:#00FF00"
    ElseIf Day(DateTime.Now) > 1 Then
        RES = "color:#FF0000;"
    End If
    Return RES
End Function

答案 1 :(得分:0)

  1. 使用for循环0到13并删除重复项
  2. 要获取7天后的日期并检查是否更早,请与date.Now.addDays(7)
  3. 比较
  4. 要检查日期是否过去,请使用date.Now.addDays(-1)

答案 2 :(得分:0)

如果必须使整个行的颜色相同,则不需要按单元格着色。请使用。

   e.Row.BackColor = System.Drawing.Color.Green;
   e.Row.ForeColor = System.Drawing.Color.White;