Search code examples
asp.netvb.netgridviewrowdatabound

Format cells in gridview boundfield columns without use of hard-coded cell index numbers


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

Solution

  • I wrote these functions years ago in C#. I used an online code converter to make them 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