0

我想知道是否有比我目前的技术更好的方法来格式化我的gridview中的单元格。Gridview 绑定到 EventItem 对象的集合类。(各种片段如下):

If e.Row.RowType = DataControlRowType.DataRow Then
        Dim dataItem As EventItem = TryCast(e.Row.DataItem, EventItem)  ' data object needs to be cast to our class
        e.Row.Cells(3).Text = UIF.showDate(dataItem.AcknowledgeDate)
        e.Row.Cells(4).Text = UIF.showDate(dataItem.LastCompletedDate)

当 gridview 的声明部分已经为绑定数据建立了序数位置时,求助于显式的单元格索引号似乎是一种耻辱:

<asp:BoundField DataField="AcknowledgeDate" HeaderText="Acknowledged Date"  DataFormatString="{0:M/d/yyyy}"></asp:BoundField>
<asp:BoundField DataField="LastCompletedDate" HeaderText="Last Completed Date" DataFormatString="{0:M/d/yyyy}"></asp:BoundField>

我正在使用此功能来避免显示为“1/1/1900”的“空”日期:

    Public Shared Function showDate(ByVal d As Date) As String
    ' without use of this function, a null date in SQL (stored as Nothing in the object), shows as 12:00:00am 
    If d = #1/1/1900# Then
        Return String.Empty
    Else
        Return d.ToString("d")  'using standard date and time format string of "short date" which is same as mm/dd/yyyy for en-US
    End If
End Function

是否可以在 gridview 的声明性部分中以某种方式使用 Eval,以便我仍然可以在函数调用中“包装”日期并删除依赖于硬编码单元格编号的代码?

最后,我的业务类对象 EventItem 将日期保存为日期,而不是字符串,它们在 SQL 表中可以为 NULL:

Public Class EventItem
    Private _LastCompletedDate As Date
    Private _AcknowledgeDate As Date

Me.LastCompletedDate = If(IsDBNull(rdr("LastCompletedDate")), Nothing, rdr("LastCompletedDate"))

 Public Property LastCompletedDate() As Date
    Get
        Return _LastCompletedDate
    End Get
    Set(ByVal value As Date)
        _LastCompletedDate = value
    End Set
End Property
4

1 回答 1

1

我在几年前用 C# 编写了这些函数。我使用在线代码转换器将它们制作成 VB。

' ---- GetCellByName ----------------------------------
'
' pass in a GridViewRow and a database column name 
' returns a DataControlFieldCell or null

Public Shared Function GetCellByName(Row As GridViewRow, CellName As [String]) As DataControlFieldCell
    For Each Cell As DataControlFieldCell In Row.Cells
        If Cell.ContainingField.ToString() = CellName Then
            Return Cell
        End If
    Next
    Return Nothing
End Function

' ---- GetColumnIndexByHeaderText ----------------------------------
'
' pass in a GridView and a Column's Header Text
' returns index of the column if found 
' returns -1 if not found 

Public Shared Function GetColumnIndexByHeaderText(aGridView As GridView, ColumnText As [String]) As Integer
    Dim Cell As TableCell
    For Index As Integer = 0 To aGridView.HeaderRow.Cells.Count - 1
        Cell = aGridView.HeaderRow.Cells(Index)
        If Cell.Text.ToString() = ColumnText Then
            Return Index
        End If
    Next
    Return -1
End Function

' ---- GetColumnIndexByDBName ----------------------------------
'
' pass in a GridView and a database field name
' returns index of the bound column if found 
' returns -1 if not found 

Public Shared Function GetColumnIndexByDBName(aGridView As GridView, ColumnText As [String]) As Integer
    Dim DataColumn As System.Web.UI.WebControls.BoundField

    For Index As Integer = 0 To aGridView.Columns.Count - 1
        DataColumn = TryCast(aGridView.Columns(Index), System.Web.UI.WebControls.BoundField)

        If DataColumn IsNot Nothing Then
            If DataColumn.DataField = ColumnText Then
                Return Index
            End If
        End If
    Next
    Return -1
End Function
于 2014-08-29T17:07:32.593 回答