How to determine Warning or Error status based on gridview date column

Viewed 60

I have a gridview with column named "Maturity Date". We need to send warning and error notification based on the "maturity Date". Conditions applied to determine Warning and error is below
a) warning = a week away from the Maturity Date and equal to Maturity Date
b) error = past the Maturity Date

My code condition is always returning true.

    `  Protected Sub DG_Importer_OnRowDataBound(ByVal sender As System.Object, ByVal e As GridViewRowEventArgs) Handles DG_Importer.RowDataBound

            Dim MaturityDate As String = DirectCast(e.Row.DataItem, System.Data.DataRowView).Row.ItemArray(3).ToString

            Dim MD As DateTime = Convert.ToDateTime(MaturityDate)
            Dim weekAwayDate As DateTime = MD.AddDays(-6)
            Dim datepassed As DateTime = MD.AddDays(+1)

            If datepassed > MD Then
                    cell.BackColor = Drawing.Color.MistyRose
                    cell.FindControl("error").Visible = True
            Else If (weekAwayDate < MD) Then
                    cell.BackColor = Drawing.Color.LightYellow
                    cell.FindControl("warning").Visible = True                         
            End If
      End Sub
1 Answers

You don't show where you defined "cell", and in fact that code would not and does not compile.

Your code should looks somthing like this:

    If e.Row.RowType = DataControlRowType.DataRow Then

        ' get the data row used to bind this row.
        Dim gData As DataRowView = e.Row.DataItem

        Dim MaturityDate As DateTime = gData("MaturityDate")

        Dim MaturityDate = gData("MaturityDate")

        Dim datepassed As DateTime = MaturityDate.AddDays(+1)

        If datepassed > MaturityDate Then
            e.Row.Cells(3).BackColor = Drawing.Color.MistyRose
            CType(e.Row.FindControl("error"), HtmlGenericControl).Visible = True
        End If

        If (weekAwayDate < MaturityDate) Then
            e.Row.Cells(3).BackColor = Drawing.Color.LightYellow
            CType(e.Row.FindControl("warning"), HtmlGenericControl).Visible = True
        End If

    End If

However, it not clear what data source the GV has, but the above code is somewhat along the lines you need.

Also, we can't see the markup, so again, it not clear if findControl should be used, or the cells collection.

the cells collection (in general) is for NON templated columns, and findcontrol is for templated controls.

Also, you can use the cells collection to get the data value, but in most cases you are better off to get the data column value from the DataRowView - which in most cases should be a table row type. And this ALSO is useful, since you NOT limited to just the columns and cells you display in the GV, but all columns are included in that DataRowView. So, often you might want to check some status value, but that column is not displayed in the GV, but exists in the data source.

But, your code looks to be a cut + paste error, or simple is wrong, as I don't see where you defined "cell" in your code, and thus your code as posted would not work, but in fact should not run at all!

as noted, I can't see your markup, so I don't know what control type to use for the error (to hide/show).

Related