我想知道是否有比我目前的技术更好的方法来格式化我的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