title: Format cells in gridview boundfield columns without use of hard-coded cell index numbers
body: <p>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):</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>&lt;asp:BoundField DataField="AcknowledgeDate" HeaderText="Acknowledged Date"  DataFormatString="{0:M/d/yyyy}"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="LastCompletedDate" HeaderText="Last Completed Date" DataFormatString="{0:M/d/yyyy}"&gt;&lt;/asp:BoundField&gt;
  </code></pre>
  
  <p>I'm using this function for avoiding an "empty" date displaying as "1/1/1900":</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>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?</p>
  
  <p>Finally, my business class object, EventItem, holds dates as dates, not strings, and they can be NULL in the SQL table:</p>
  
  <pre><code>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
  </code></pre>