代码之家  ›  专栏  ›  技术社区  ›  G_Hosa_Phat

是否存在数据单元类

  •  0
  • G_Hosa_Phat  · 技术社区  · 7 年前

    我有两个实用函数,用于测试“空单元格”(定义为 NULL 或 空的 String 价值)。一个换一个 DataGridViewCell DataTable 牢房。一般来说,这些方法似乎工作得很好,但我目前必须调用该方法来测试 数据表 当测试空单元格的方法 单元格 只需要测试特定的对象。

    Public Shared Function IsCellEmpty(ByVal Cell As DataGridViewCell) As Boolean
        If Cell.Value Is Nothing OrElse Cell.Value Is DBNull.Value OrElse String.IsNullOrEmpty(Cell.Value.ToString) Then
            Return True
        Else
            Return False
        End If
    End Function
    
    Public Shared Function IsCellEmpty(ByVal Row As DataRow, ByVal ColumnIndex As Integer) As Boolean
        If Row(ColumnIndex) Is DBNull.Value OrElse String.IsNullOrEmpty(Row(ColumnIndex).ToString.Trim) Then
            Return True
        Else
            Return False
        End If
    End Function
    
    Public Shared Function IsCellEmpty(ByVal Row As DataRow, ByVal ColumnName As String) As Boolean
        If Row(ColumnName) Is DBNull.Value OrElse String.IsNullOrEmpty(Row(ColumnName).ToString.Trim) Then
            Return True
        Else
            Return False
        End If
    End Function
    
    ' *** USAGE EXAMPLE ***
    For Each CurrentRow As DataRow In MyDataTable.Rows
        If Not Utility.IsCellEmpty(CurrentRow, "CellName") Then
            ' Do Something Here...
        End If
    
        If Not Utility.IsCellEmpty(CurrentRow, 5) Then
            ' Do Something Here...
        End If
    Next CurrentRow
    
    ' *** WHAT I'D LIKE TO SEE ***
    For Each CurrentRow2 As DataRow In MyDataTable.Rows
        If Not Utility.IsCellEmpty(CurrentRow("CellName")) Then
            ' Do Something Here...
        End If
    
        If Not Utility.IsCellEmpty(CurrentRow(5)) Then
            ' Do Something Here...
        End If
    
        ' *** OR ***
        For Each CurrentCell As DataCell In CurrentRow.Cells
            If Utility.IsCellEmpty(CurrentCell) Then
                ' Do Something Here...
            End If
        Next CurrentCell
    Next CurrentRow2
    

    那么,为了给标题中的问题提供一点更具体的说明,是否有 DataCell 类作为 数据表 可以类似于 单元格 需要 ,但我想我会问,以防其他人也有同样的问题。在我所做的搜索中,我一直无法找到这个特定的对象类,但我确信这里的人至少可以为我指明正确的方向。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Tim Schmelter    7 年前

    不,没有这样的事 DataCell . 有一个 DataColumn ,但这只是存储列信息,如名称和类型,而不是表的某一行中的值。

    DataRow 存储所有字段,以便您可以像以前一样传递该字段。您应该提供一个不依赖于特定框架的winforms类型,而不是将winforms类型传递给此方法( DBNull Object . 然后你可以用这个做你的 DataGridViewCell 同时也是为了 -场。

    Public Shared Function IsValueNullOrEmpty(ByVal value As Object) As Boolean
        Return value Is DBNull.Value OrElse String.IsNullOrEmpty(value?.ToString()) 
    End Function
    
    Public Shared Function IsCellValueNullOrEmpty(ByVal cell As DataGridViewCell) As Boolean
        Return IsValueNullOrEmpty(cell?.Value?.ToString())
    End Function
    
    Public Shared Function IsFieldNullOrEmpty(ByVal row As DataRow, ByVal columnIndex As Integer) As Boolean
        Return IsValueNullOrEmpty(row?(columnIndex))
    End Function
    
    Public Shared Function IsFieldNullOrEmpty(ByVal row As DataRow, ByVal columnName As String) As Boolean
        Return IsValueNullOrEmpty(row?(columnName))
    End Function