I'm wanting to know if there might be a better way to format cells in my gridview than my current technique. Gridview is bound to a collection class of EventItem objects. (various snippets follow):
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)
It seems a shame to resort to explicit cell index numbers when the declarative part of the gridview already establishes the ordinal position for the bound data:
<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>
I'm using this function for avoiding an "empty" date displaying as "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
Could Eval somehow be used in the declarative part of the gridview so that I could still "wrap" the date in the function call and remove the code dependent upon hardcoded cell numbers?
Finally, my business class object, EventItem, holds dates as dates, not strings, and they can be NULL in the SQL table:
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