0
我想知道是否可能有比我當前的技術更好的格式化單元格的GridView中的單元格。 Gridview綁定到一個EventItem對象的集合類。 (各種片段如下):在沒有使用硬編碼的單元格索引號的情況下在gridview boundfield列中設置單元格的格式
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的聲明部分使用,這樣我可以仍然在函數調用中「包裝」日期並刪除取決於硬編碼單元號的代碼?
最後,我的業務類對象,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
謝謝Steve。看起來非常好。不同於我想象的可能。我希望能夠在Gridview中利用Boundfields的自然序數位置。我已經看到Eval在那裏使用,但看不到如何去做。 – 2014-08-29 20:08:23