Dynamically color a gridview vb.net

I am using VS 2008, I have a gridview on an aspx page, I am filling the gv with data, one of the columns is Order Date. I want to change the background color of this column depending on how old the order is.

So if the order is < 10 days it would be green,
if the order is 11 - 30 days it would be yellow anything older would be red.

Here is wh at I am trying but I am stuck...

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myorderdate As Date
        For Each row As GridViewRow In GridView1.Rows
            myorderdate = row.Cells.Item("OrderDate").Text
            If myorderdate = Now() Then


            End If

        Next
    End Sub
End Class
LVL 1
Wildone63Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

13598Commented:
In your CellFormatting event enter something like this:
If Me.GridView1.Rows(e.rowindex).Cells("OrderDate").Value < Now.Date.AddDays(10)Then
                Me.GridView1.Rows(e.rowindex).DefaultCellStyle.BackColor = Color.Green
else
If Me.GridView1.Rows(e.rowindex).Cells("OrderDate").Value  "Y" Then
                Me.GridView1.Rows(e.rowindex).DefaultCellStyle.BackColor = Color.Red
end if
end if
0
Wildone63Author Commented:
Please excuse my ignorance, but what and or where is a cellformatting event?

Thanks
0
13598Commented:
Oops. Here it is
In your CellFormatting event enter something like this:
If Me.GridView1.Rows(e.rowindex).Cells("OrderDate").Value < Now.Date.AddDays(10)Then
                Me.GridView1.Rows(e.rowindex).DefaultCellStyle.BackColor = Color.Green
else
If Me.GridView1.Rows(e.rowindex).Cells("OrderDate").Value  > Now.Date.AddDays(10)
andalso Me.GridView1.Rows(e.rowindex).Cells("OrderDate").Value  <=  Now.Date.AddDays(30) Then
                Me.GridView1.Rows(e.rowindex).DefaultCellStyle.BackColor = Color.Red
end if
end if
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

13598Commented:
If you have a gridview1 you more than likely have a
 Private Sub gridview1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles gridview1.CellFormatting
        Try
If Me.GridView1.Rows(e.rowindex).Cells("OrderDate").Value < Now.Date.AddDays(10)Then
                Me.GridView1.Rows(e.rowindex).DefaultCellStyle.BackColor = Color.Green
else
If Me.GridView1.Rows(e.rowindex).Cells("OrderDate").Value  > Now.Date.AddDays(10)
andalso Me.GridView1.Rows(e.rowindex).Cells("OrderDate").Value  <=  Now.Date.AddDays(30) Then
                Me.GridView1.Rows(e.rowindex).DefaultCellStyle.BackColor = Color.Red
end if
end if
                    Catch ex As Exception
messagebox.show(ex.message)
        End Try
    End Sub
0
Wildone63Author Commented:
No, I do not have that?
0
13598Commented:
OK then.   Try using this:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myorderdate As Date
        For Each row As GridViewRow In GridView1.Rows
            myorderdate = row.Cells.Item("OrderDate").Text
            If myorderdate = < Now.Date.AddDays(10)Then
                row.DefaultCellStyle.BackColor = Color.Green
else
If myorderdate > Now.Date.AddDays(10)
andalso myorderdate <=  Now.Date.AddDays(30) Then
                row.DefaultCellStyle.BackColor = Color.Red
end if
            End If

        Next
    End Sub
0
13598Commented:
OK then.   Try using this:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myorderdate As Date
        For Each row As GridViewRow In GridView1.Rows
            myorderdate = row.Cells.Item("OrderDate").Text
            If myorderdate  < Now.Date.AddDays(10)Then
                row.DefaultCellStyle.BackColor = Color.Green
else
If myorderdate > Now.Date.AddDays(10)
andalso myorderdate <=  Now.Date.AddDays(30) Then
                row.DefaultCellStyle.BackColor = Color.Red
end if
            End If

        Next
    End Sub


   

0
13598Commented:
Try this:
Dim myorderdate As Date
        For Each row As GridViewRow In GridView1.Rows
            myorderdate = row.Cells.Item("OrderDate").Text
            If myorderdate < Now.Date.AddDays(10) Then
                row.BackColor = Drawing.Color.Green
            Else
                If myorderdate > Now.Date.AddDays(10) And myorderdate <= Now.Date.AddDays(30) Then

                    row.BackColor = Drawing.Color.Red
                End If
            End If

        Next
0
13598Commented:
If you have a rowdatabound event it would probably be better to have color formatting there.
0
13598Commented:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

   If e.Row.RowType = DataControlRowType.DataRow Then

      If e.Row.DataItem("OrderDate") < Now.Date.AddDays(10)  Then
         e.Row.BackColor = Drawing.Color.Green
      End If
   If e.Row.DataItem("OrderDate") > Now.Date.AddDays(10) And e.Row.DataItem("OrderDate") <= Now.Date.AddDays(30) Then

         e.Row.BackColor = Drawing.Color.Red
      End If

   End If

End Sub

0
Wildone63Author Commented:
The last one seems to be right, but how do I call the rowdatabound event?
0
13598Commented:
Handles GridView1.RowDataBound  at the end of it should do it, like this:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
0
Wildone63Author Commented:
This is Almost there, I setup things like you said but it is turning the whole row red or green, I only want to change the cell OrderDate color.
Thank YOU!
0
13598Commented:
If you only want to color just the orderdate cell instead of the whole row and you know the cell index, then this should work:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound


   If e.Row.RowType = DataControlRowType.DataRow Then

      If e.Row.DataItem("OrderDate") < Now.Date.AddDays(10)  Then
         e.Row.Cells(cellindexhere).BackColor = Drawing.Color.Green
      End If
   If e.Row.DataItem("OrderDate") > Now.Date.AddDays(10) And e.Row.DataItem("OrderDate") <= Now.Date.AddDays(30) Then

         e.Row.Cells(cellindexhere).BackColor = Drawing.Color.Red
      End If

   End If

End Sub
0
Wildone63Author Commented:
I get the error cellindexhere is not declared...
0
13598Commented:
Sorry, I meant you need to enter your cell index there. What cell number is Orderdate, i.e. if it is the 3rd cell then your cell index would be 3 as in
  e.Row.Cells(3).BackColor = Drawing.Color.Red
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wildone63Author Commented:
Thank You Very Much for your assistance! That worked fine.

There is an issue with null dates but that is another issue!

Thanks Again!
0
13598Commented:
Check for null date like this:
if (e.row.dataItem,"OrderDate") = null then
''  do whatever
else
' do the coloring.
0
Wildone63Author Commented:
Thank You!!!! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.