question

PhillipVong-0946 avatar image
0 Votes"
PhillipVong-0946 asked PhillipVong-0946 commented

Count days out of gridview row databound in one cell

First - Thanks for helping a newbie. Doing this in VB.net / asp.net.

Goal - During a GridView RowDataBound, I want to...

Check each row and see if Column 5 is Blank
If not blank
convert to date and see if that date is less than 365 of today
If less than 365, make that cell backcolor yellow

Col(5) data type in SQL is DATE
Col(5) DataFormatString in Gridview is {0:d}

My Code:

  Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
         If Not e.Row.Cells(5).ToString = String.Empty Then
    
             Dim CoDateToDate As Date = Convert.ToDateTime(e.Row.Cells(5))
             Dim Ct As Int32 = DateDiff(DateInterval.Day, CoDateToDate.Day, Now.Day)
    
             If Ct < 365 Then
                 e.Row.Cells(5).BackColor = Drawing.Color.Yellow
             End If
         End If
    
    
     End Sub


My Error Message
'Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldHeaderCell' to type 'System.IConvertible'.'

My Imports - don't know if I'm not importing the right things.
Imports System.Data, System.Data.SqlClient, System.Web.UI.WebControls

I didn't see a way to CONVERT to Date. I can only Convert to DateTime. Is this the problem with the SQL datatype is just DATE?

Please help and thanks!




dotnet-visual-basicdotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlbertKallal-4360 avatar image
0 Votes"
AlbertKallal-4360 answered PhillipVong-0946 commented

What you have looks ok - but when the data bound triggers, it ALSO will include the heading and the footing grid row - you ONLY want to process/run your code for a data row.

so, wrap your existing code in this:

     If e.Row.RowType = DataControlRowType.DataRow Then

     End If

Regards,
Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada


· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm still getting the same error code after I wrap it all in what you said. Here's my updated code.

 Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
         If e.Row.RowType = DataControlRowType.DataRow Then
             If Not e.Row.Cells(5).ToString = String.Empty Then
    
                 Dim CoDateToDate As Date = Convert.ToDateTime(e.Row.Cells(5))
                 Dim Ct As Int32 = DateDiff(DateInterval.Day, CoDateToDate.Day, Now.Day)
    
                 If Ct < 365 Then
                     e.Row.Cells(5).BackColor = Drawing.Color.Yellow
                 End If
             End If
         End If
    
     End Sub


This is the line that's triggering the error.
Dim CoDateToDate As Date = Convert.ToDateTime(e.Row.Cells(5))

Details of the error below
$exception {"Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.IConvertible'."} System.InvalidCastException



0 Votes 0 ·

try:

        If e.Row.RowType = DataControlRowType.DataRow Then

              If e.Row.Cells(5).Text <> "" Then
        
                  Dim CoDateToDate As Date = e.Row.Cells(5).Text
                  Dim Ct As Int32 = DateDiff(DateInterval.Day, Date.Today,CoDateToDate)
        
                  If Ct < 365 Then
                      e.Row.Cells(5).BackColor = Drawing.Color.Yellow
                  End If
              End If
          End If
0 Votes 0 ·

I'm getting the below error trying to convert the text to date.

Dim CoDateToDate As Date = e.Row.Cells(5).Text

System.InvalidCastException: 'Conversion from string "&nbsp;" to type 'Date' is not valid.'

0 Votes 0 ·
Show more comments