Link to home
Start Free TrialLog in
Avatar of Wildone63
Wildone63

asked on

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
Avatar of 13598
13598
Flag of United States of America image

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
Avatar of Wildone63
Wildone63

ASKER

Please excuse my ignorance, but what and or where is a cellformatting event?

Thanks
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
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
No, I do not have that?
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
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


   

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
If you have a rowdatabound event it would probably be better to have color formatting there.
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

The last one seems to be right, but how do I call the rowdatabound event?
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
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!
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
I get the error cellindexhere is not declared...
ASKER CERTIFIED SOLUTION
Avatar of 13598
13598
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You Very Much for your assistance! That worked fine.

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

Thanks Again!
Check for null date like this:
if (e.row.dataItem,"OrderDate") = null then
''  do whatever
else
' do the coloring.
Thank You!!!! :)