• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9607
  • Last Modified:

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.

<%#PastDue(Eval("Latest"))%>

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?
0
JRockFL
Asked:
JRockFL
  • 3
2 Solutions
 
samtran0331Commented:
...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 = ""
            Else
                e.Row.CssClass = ""
            End If
        End If
    End Sub
0
 
RejojohnyCommented:
I think what samtran0331 meant was
    If dt < dtNow Then
                e.Row.CssClass = "pastdue"
0
 
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">
                                            <ItemTemplate>
                                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("LATEST", "{0:d}") %>'></asp:Label>
                                            </ItemTemplate>
                                            <HeaderStyle HorizontalAlign="Left" />
                                        </asp:TemplateField>
0
 
JRockFLAuthor Commented:
How we doing? Any ideas?
0
 
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now