摘要
在「GridView 加入自动编号字段」一文有提到如何在 GridView 中利用 TemplateField 来加入自动编号;本文将改用另一种方式,利用继承 DataControlField 来撰写自动编号字段,若 GridView 需要自动编号字段时只需加入字段即可。
TBSerialNumberField 字段
继承 DataControlField 命名为 TBSerialNumberField,覆写 InitializeCell 方法,判断 CellType = DataControlCellType.DataCell 时就执行 InitializeDataCell 方法来产生自动编号。
Imports
System.Web.UI.WebControls
Namespace WebControls Namespace WebControls
Public Class TBSerialNumberFieldClass TBSerialNumberField
Inherits DataControlField
Private FRowIndex As Integer = 0
Protected Overrides Function CreateField()Function CreateField() As System.Web.UI.WebControls.DataControlField
Return New TBSerialNumberField()
End Function
/**/''' <summary>
''' 储存格初始化。
''' </summary>
''' <param name="Cell">要初始化的储存格。</param>
''' <param name="CellType">储存格类型。</param>
''' <param name="RowState">数据列状态。</param>
''' <param name="RowIndex">数据列之以零起始的索引。</param>
Public Overrides Sub InitializeCell()Sub InitializeCell(ByVal Cell As DataControlFieldCell, ByVal CellType As DataControlCellType, _
ByVal RowState As DataControlRowState, ByVal RowIndex As Integer)
FRowIndex = RowIndex
MyBase.InitializeCell(Cell, CellType, RowState, RowIndex)
If (CellType = DataControlCellType.DataCell) Then
Me.InitializeDataCell(Cell, RowState)
End If
End Sub
/**/''' <summary>
''' 数据储存格初始化。
''' </summary>
''' <param name="Cell">要初始化的储存格。</param>
''' <param name="RowState">数据列状态。</param>
Protected Overridable Sub InitializeDataCell()Sub InitializeDataCell(ByVal Cell As DataControlFieldCell, ByVal RowState As DataControlRowState)
Dim iDataRowIndex As Integer
iDataRowIndex = GetDataRowIndex()
Cell.Text = (iDataRowIndex + 1).ToString
End Sub
/**/''' <summary>
''' 取得数据列索引。
''' </summary>
Private Function GetDataRowIndex()Function GetDataRowIndex() As Integer
Dim oGridView As GridView
If TypeOf Me.Control Is GridView Then
oGridView = DirectCast(Me.Control, GridView)
If oGridView.AllowPaging Then
Return oGridView.PageIndex * oGridView.PageSize + FRowIndex
Else
Return FRowIndex
End If
Else
Return FRowIndex
End If
End Function
End Class
End Namespace
Namespace WebControls Namespace WebControls
Public Class TBSerialNumberFieldClass TBSerialNumberField
Inherits DataControlField
Private FRowIndex As Integer = 0
Protected Overrides Function CreateField()Function CreateField() As System.Web.UI.WebControls.DataControlField
Return New TBSerialNumberField()
End Function
/**/''' <summary>
''' 储存格初始化。
''' </summary>
''' <param name="Cell">要初始化的储存格。</param>
''' <param name="CellType">储存格类型。</param>
''' <param name="RowState">数据列状态。</param>
''' <param name="RowIndex">数据列之以零起始的索引。</param>
Public Overrides Sub InitializeCell()Sub InitializeCell(ByVal Cell As DataControlFieldCell, ByVal CellType As DataControlCellType, _
ByVal RowState As DataControlRowState, ByVal RowIndex As Integer)
FRowIndex = RowIndex
MyBase.InitializeCell(Cell, CellType, RowState, RowIndex)
If (CellType = DataControlCellType.DataCell) Then
Me.InitializeDataCell(Cell, RowState)
End If
End Sub
/**/''' <summary>
''' 数据储存格初始化。
''' </summary>
''' <param name="Cell">要初始化的储存格。</param>
''' <param name="RowState">数据列状态。</param>
Protected Overridable Sub InitializeDataCell()Sub InitializeDataCell(ByVal Cell As DataControlFieldCell, ByVal RowState As DataControlRowState)
Dim iDataRowIndex As Integer
iDataRowIndex = GetDataRowIndex()
Cell.Text = (iDataRowIndex + 1).ToString
End Sub
/**/''' <summary>
''' 取得数据列索引。
''' </summary>
Private Function GetDataRowIndex()Function GetDataRowIndex() As Integer
Dim oGridView As GridView
If TypeOf Me.Control Is GridView Then
oGridView = DirectCast(Me.Control, GridView)
If oGridView.AllowPaging Then
Return oGridView.PageIndex * oGridView.PageSize + FRowIndex
Else
Return FRowIndex
End If
Else
Return FRowIndex
End If
End Function
End Class
End Namespace
测试程序
当 GridView 需要有自动编号字段时,只有加入 TBSerialNumberField 即可。
<
Columns
>
< bee:TBSerialNumberField HeaderText ="No" ></ bee:TBSerialNumberField >
< asp:BoundField DataField ="LastName" HeaderText ="LastName" SortExpression ="LastName" />
< asp:BoundField DataField ="FirstName" HeaderText ="FirstName" SortExpression ="FirstName" />
< asp:BoundField DataField ="Title" HeaderText ="Title" SortExpression ="Title" />
< asp:BoundField DataField ="EmployeeID" HeaderText ="EmployeeID" InsertVisible ="False"
ReadOnly ="True" SortExpression ="EmployeeID" />
</ Columns >
< bee:TBSerialNumberField HeaderText ="No" ></ bee:TBSerialNumberField >
< asp:BoundField DataField ="LastName" HeaderText ="LastName" SortExpression ="LastName" />
< asp:BoundField DataField ="FirstName" HeaderText ="FirstName" SortExpression ="FirstName" />
< asp:BoundField DataField ="Title" HeaderText ="Title" SortExpression ="Title" />
< asp:BoundField DataField ="EmployeeID" HeaderText ="EmployeeID" InsertVisible ="False"
ReadOnly ="True" SortExpression ="EmployeeID" />
</ Columns >
执行结果如下