2014-08-29 76 views
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 

回答

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 
+0

謝謝Steve。看起來非常好。不同於我想象的可能。我希望能夠在Gridview中利用Boundfields的自然序數位置。我已經看到Eval在那裏使用,但看不到如何去做。 – 2014-08-29 20:08:23

相關問題