March 2, 2009

GridView加入流水號的方法 - 使用伺服器控制項(2)

在「GridView加入流水號的方法 - 使用伺服器控制項(1)」,大家測試時會發現,如果GridView有分頁,流水號會重新計算。
在某些情況下,可能需要將流水號累加,這時可以將覆寫的InitializeCell(ByVal cell As System.Web.UI.WebControls.DataControlFieldCell, ByVal cellType As System.Web.UI.WebControls.DataControlCellType, ByVal rowState As System.Web.UI.WebControls.DataControlRowState, ByVal rowIndex As Integer)修改為如下程式碼
Public Overrides Sub InitializeCell(ByVal cell As System.Web.UI.WebControls.DataControlFieldCell, ByVal cellType As System.Web.UI.WebControls.DataControlCellType, ByVal rowState As System.Web.UI.WebControls.DataControlRowState, ByVal rowIndex As Integer)
  MyBase.InitializeCell(cell, cellType, rowState, rowIndex)
  If cellType = DataControlCellType.DataCell Then
    cell.Text = (OwnerGridView.PageIndex * OwnerGridView.PageSize + rowIndex + 1).ToString()
  End If
End Sub Private ReadOnly Property OwnerGridView() As GridView   Get     Return DirectCast(Me.Control, GridView)   End Get End Property
若需同時支援累加和不累加流水號,可自行加入屬性做判斷。

No comments: