0

I have a column with dates and strings in my table (varchar) named DateColumn and i want to show it as dates to be able to filter by date. I first tried to create an unbound column with unboundexpression with GetData(DateColumn) and it worked, all the strings with dates where converted to date and the other was #Err. And i was able to filter by date. but i wanted to control these #Err: its empty? its "otherstring"? or its something other? so i tried with CustomUnboundColumnData function:

           Try
               value = Date.Parse(view.GetListSourceRowCellValue(listSourceRowIndex, columna))
           Catch ex As Exception
               If IsDBNull(view.GetListSourceRowCellValue(listSourceRowIndex, columna)) Then
                   value = DBNull.Value
               Else
                   If view.GetListSourceRowCellValue(listSourceRowIndex, columna) = "" Then
                       value = DBNull.Value
                   ElseIf view.GetListSourceRowCellValue(listSourceRowIndex, columna) = "otherstring" Then
                       value = "#otherstring"
                   Else
                       value = "#Err"
                   End If
               End If

           End Try 

but now when I try to filter by value (by date) i get an error:

Cannot compare two items in array.: System.ArgumentException: The object must be of type String.

if I replace the "#Err" and "#otherstring" for DBNull.Value it works again. but i need to be able to put other strings.

  • It's not quite clear what you are trying to do. Please show one or two sample data and tell what the expected behavior is based on the sample data. – Reza Aghaei Jan 17 '22 at 00:10

1 Answers1

0

Don't use the Try...End Try for fully expected errors. You are not using ex at all.

Use a TryParse. You can distinguish the nulls and other strings by assigning an obviously bogus date. Create a list of the bad strings for review.

    Dim d As Date
    Dim value As Date
    Dim errList As New List(Of String)
    If Date.TryParse(View.GetListSourceRowCellValue(listSourceRowIndex, columna), d) Then
        value = d
    ElseIf IsDBNull(View.GetListSourceRowCellValue(listSourceRowIndex, columna)) Then
        value = CDate("January 2, 0001 ")
    ElseIf View.GetListSourceRowCellValue(listSourceRowIndex, columna) = "" Then
        value = CDate("January 2, 0001 ")
    Else
        value = CDate("January 1, 0001")
        errList.Add(View.GetListSourceRowCellValue(listSourceRowIndex, columna).ToString)
    End If
Mary
  • 14,926
  • 3
  • 18
  • 27