从 DBNull 类型到 String 类型的转换在 vb.net 中无效

Conversion from type DBNull to type String is not valid in vb.net

请解决我遇到错误“从类型 'DBNull' 转换为类型 'String' 因为 pathhelper 列具有空值

Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
        If e.Column.FieldName = "Image" AndAlso e.IsGetData Then
            Dim view As GridView = TryCast(sender, GridView)

            Dim colorName As String = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Color"))
            Dim fileName As String = GetFileName(colorName).ToLower()
            ***Dim PATHHELPER As String = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper")) 'error this line***


            If (Not Images.ContainsKey(fileName)) Then
                Dim img As Image = Nothing
                Try
                    Dim filePath As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & PATHHELPER, fileName, False)
                    img = Image.FromFile(filePath)
                Catch
                End Try
                Images.Add(fileName, img)
            End If
            e.Value = Images(fileName)
        End If
    End Sub
End Class

我认为像这样的东西应该可以解决问题。可能更好的方法来做到这一点,但这是我想要的方式

Dim PATHHELPER as String = String.Empty
If view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper") <> DBNull.Value Then
    PATHHELPER = view.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper").ToString
End if

使用 DBNull class 值 属性 进行比较。

Dim PATHHELPER As String
If Not DBNull.Value.Equals(View.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper")) Then
    PATHHELPER = CStr(View.GetListSourceRowCellValue(e.ListSourceRowIndex, "Pathhelper"))
Else
    PATHHELPER = String.Empty 'or Nothing depending what comes next.
End If