【问题标题】:Format cells in gridview boundfield columns without use of hard-coded cell index numbers在gridview boundfield列中格式化单元格而不使用硬编码的单元格索引号
【发布时间】:2014-08-29 16:14:30
【问题描述】:

我想知道是否有比我目前的技术更好的方法来格式化我的 gridview 中的单元格。 Gridview 绑定到 EventItem 对象的集合类。 (后面有各种sn-ps):

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

【问题讨论】:

    标签: asp.net vb.net gridview rowdatabound


    【解决方案1】:

    我多年前用 C# 编写了这些函数。我使用在线代码转换器将它们制作为 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
    

    【讨论】:

    • 谢谢史蒂夫。看起来非常好。与我想象的不同的方法。我希望能够利用 Gridview 中 Boundfields 的自然序数位置。我已经看到那里使用了 Eval,但不知道该怎么做。
    猜你喜欢
    • 2013-10-30
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多