在gridview boundfield列中格式化单元格而不使用硬编码的单元格索引号

时间:2014-08-29 16:14:30

标签: asp.net vb.net gridview rowdatabound

我想知道在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

是否可以在网格视图的声明部分中使用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

1 个答案:

答案 0 :(得分: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
相关问题