Apply css style to certain rows in gridview

Posted on 2006-04-04
Last Modified: 2008-02-21
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?
Question by:JRockFL
    LVL 37

    Accepted Solution

    ...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
    LVL 26

    Assisted Solution

    I think what samtran0331 meant was
        If dt < dtNow Then
                    e.Row.CssClass = "pastdue"
    LVL 8

    Author Comment

    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" />
    LVL 8

    Author Comment

    How we doing? Any ideas?
    LVL 8

    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now