Apply css style to certain rows in gridview

How can I apply a css style to certain rows in a gridview?

The gridview has a date column called latest, and I am going to compare that to Now() with a helper function. If the latest date is less then now, then I would like to apply a style to it.


Here is my function
Public Function PastDue(ByVal latest As Date) As String
        Dim now As Date = Date.Now
        Dim cssStyle As String = String.Empty

        If latest < now Then
            cssStyle = "pastdue"
        End If

        Return cssStyle

    End Function

Where and how in the gridview do I apply it?
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.

...i'd do it in the rowdatabound of the grid...something like:
should also be able to use findcontrol if the "latest" is in a control...

    Private dt As Date
    Private dtNow As Date = Now.Date
    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType <> Web.UI.WebControls.DataControlRowType.Header And e.Row.RowType <> Web.UI.WebControls.DataControlRowType.Footer Then
            dt = CType(Eval("latest"), Date)
            If dt < dtNow Then
                e.Row.CssClass = ""
                e.Row.CssClass = ""
            End If
        End If
    End Sub

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
I think what samtran0331 meant was
    If dt < dtNow Then
                e.Row.CssClass = "pastdue"
JRockFLAuthor Commented:
Thank you for the replies.

Here is the error message I get
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
Line 72:             dtLatest = CType(Eval("latest"), Date)

This is "latest" from the gridview. I did it as template field, because I couldn't get the date to format correctly. No matter what I did, I couldn't get it to not display the time. Is it a .NET 2.0 bug?

                                        <asp:TemplateField HeaderText="Latest">
                                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("LATEST", "{0:d}") %>'></asp:Label>
                                            <HeaderStyle HorizontalAlign="Left" />
JRockFLAuthor Commented:
How we doing? Any ideas?
JRockFLAuthor Commented:
I figured it out myself, now gimme back my points. Just kidding LOL

Thank you very much!

Protected Sub gvOnOrder_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvOnOrder.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim mrpDate As Label = CType(e.Row.FindControl("label3"), Label)
            If CType(mrpDate.Text, Date) < dtNow Then
                e.Row.CssClass = "pastdue"
            End If
        End If
    End Sub
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

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.