Reputation: 21
I would like to allow my Gridview control to display various colours based on conditions. A person has a date for a medical. I want the gridview to show a green color when the day has arrived. I want the date to show red when one day has passed on from the due date and orange like 7 days before the medical date. I have only managed to fix it for the green color which shows the medical on the day but I am having difficulties for the other conditions.
This is what i currently have. The first part of the If Statement works when i change the row to green based on the due date and the current date that is equal but I do not know how to set the if statement for the other conditions mentioned above. Please assist
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
Upvotes: 0
Views: 159
Reputation: 645
date.Now.addDays(7)
date.Now.addDays(-1)
Upvotes: 1
Reputation: 419
If you have to make the entire row to be colored the same, then cellwise coloring is not required. Please use.
e.Row.BackColor = System.Drawing.Color.Green;
e.Row.ForeColor = System.Drawing.Color.White;
Upvotes: 1
Reputation: 1
You can use code behind method as follows:
Add the template column in GridView like this:
<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>
and use the following method for each cell:
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
Upvotes: 0