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
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"
If myorderdate = Now() Then
End If
Next
End Sub
End Class
ASKER
Please excuse my ignorance, but what and or where is a cellformatting event?
Thanks
Thanks
Oops. Here it is
In your CellFormatting event enter something like this:
If Me.GridView1.Rows(e.rowind ex).Cells( "OrderDate ").Value < Now.Date.AddDays(10)Then
Me.GridView1.Rows(e.rowind ex).Defaul tCellStyle .BackColor = Color.Green
else
If Me.GridView1.Rows(e.rowind ex).Cells( "OrderDate ").Value > Now.Date.AddDays(10)
andalso Me.GridView1.Rows(e.rowind ex).Cells( "OrderDate ").Value <= Now.Date.AddDays(30) Then
Me.GridView1.Rows(e.rowind ex).Defaul tCellStyle .BackColor = Color.Red
end if
end if
In your CellFormatting event enter something like this:
If Me.GridView1.Rows(e.rowind
Me.GridView1.Rows(e.rowind
else
If Me.GridView1.Rows(e.rowind
andalso Me.GridView1.Rows(e.rowind
Me.GridView1.Rows(e.rowind
end if
end if
If you have a gridview1 you more than likely have a
Private Sub gridview1_CellFormatting(B yVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewCel lFormattin gEventArgs ) Handles gridview1.CellFormatting
Try
If Me.GridView1.Rows(e.rowind ex).Cells( "OrderDate ").Value < Now.Date.AddDays(10)Then
Me.GridView1.Rows(e.rowind ex).Defaul tCellStyle .BackColor = Color.Green
else
If Me.GridView1.Rows(e.rowind ex).Cells( "OrderDate ").Value > Now.Date.AddDays(10)
andalso Me.GridView1.Rows(e.rowind ex).Cells( "OrderDate ").Value <= Now.Date.AddDays(30) Then
Me.GridView1.Rows(e.rowind ex).Defaul tCellStyle .BackColor = Color.Red
end if
end if
Catch ex As Exception
messagebox.show(ex.message )
End Try
End Sub
Private Sub gridview1_CellFormatting(B
Try
If Me.GridView1.Rows(e.rowind
Me.GridView1.Rows(e.rowind
else
If Me.GridView1.Rows(e.rowind
andalso Me.GridView1.Rows(e.rowind
Me.GridView1.Rows(e.rowind
end if
end if
Catch ex As Exception
messagebox.show(ex.message
End Try
End Sub
ASKER
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.BackC olor = Color.Green
else
If myorderdate > Now.Date.AddDays(10)
andalso myorderdate <= Now.Date.AddDays(30) Then
row.DefaultCellStyle.BackC olor = Color.Red
end if
End If
Next
End Sub
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"
If myorderdate = < Now.Date.AddDays(10)Then
row.DefaultCellStyle.BackC
else
If myorderdate > Now.Date.AddDays(10)
andalso myorderdate <= Now.Date.AddDays(30) Then
row.DefaultCellStyle.BackC
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.BackC olor = Color.Green
else
If myorderdate > Now.Date.AddDays(10)
andalso myorderdate <= Now.Date.AddDays(30) Then
row.DefaultCellStyle.BackC olor = Color.Red
end if
End If
Next
End Sub
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"
If myorderdate < Now.Date.AddDays(10)Then
row.DefaultCellStyle.BackC
else
If myorderdate > Now.Date.AddDays(10)
andalso myorderdate <= Now.Date.AddDays(30) Then
row.DefaultCellStyle.BackC
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
Dim myorderdate As Date
For Each row As GridViewRow In GridView1.Rows
myorderdate = row.Cells.Item("OrderDate"
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(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs )
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
If e.Row.RowType = DataControlRowType.DataRow
If e.Row.DataItem("OrderDate"
e.Row.BackColor = Drawing.Color.Green
End If
If e.Row.DataItem("OrderDate"
e.Row.BackColor = Drawing.Color.Red
End If
End If
End Sub
ASKER
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(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) Handles GridView1.RowDataBound
Protected Sub GridView1_RowDataBound(ByV
ASKER
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!
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(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) 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
Protected Sub GridView1_RowDataBound(ByV
If e.Row.RowType = DataControlRowType.DataRow
If e.Row.DataItem("OrderDate"
e.Row.Cells(cellindexhere)
End If
If e.Row.DataItem("OrderDate"
e.Row.Cells(cellindexhere)
End If
End If
End Sub
ASKER
I get the error cellindexhere is not declared...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You Very Much for your assistance! That worked fine.
There is an issue with null dates but that is another issue!
Thanks Again!
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.
if (e.row.dataItem,"OrderDate
'' do whatever
else
' do the coloring.
ASKER
Thank You!!!! :)
If Me.GridView1.Rows(e.rowind
Me.GridView1.Rows(e.rowind
else
If Me.GridView1.Rows(e.rowind
Me.GridView1.Rows(e.rowind
end if
end if