posts - 71, comments - 301, trackbacks - 22, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

GridView 自动编号字段 - TBSerialNumberField

Posted on 2008-05-29 22:02 jeff377 阅读(1724) 评论(7)  编辑 收藏 所属分类: Server Control

摘要
在「GridView 加入自动编号字段」一文有提到如何在 GridView 中利用 TemplateField 来加入自动编号;本文将改用另一种方式,利用继承 DataControlField 来撰写自动编号字段,若 GridView 需要自动编号字段时只需加入字段即可。

TBSerialNumberField 字段
继承 DataControlField 命名为 TBSerialNumberField,覆写 InitializeCell 方法,判断 CellType = DataControlCellType.DataCell 时就执行 InitializeDataCell 方法来产生自动编号。

Imports System.Web.UI.WebControls

Namespace WebControls
    
Public Class TBSerialNumberField
        
Inherits DataControlField

        
Private FRowIndex As Integer = 0

        
Protected Overrides 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(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(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() 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>

执行结果如下



Feedback

#1楼    回复  引用  查看    

2008-05-29 23:53 by kiler      
原文回复中的方案就不错
<asp:TemplateField HeaderText="序號">
<ItemTemplate>
<%#GridView1.PageIndex * GridView1.PageSize + GridView1.Rows.Count + 1%>
</ItemTemplate>
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:TemplateField>
为这点小功能写个控件,有点多余。

#2楼    回复  引用  查看    

2008-05-30 00:26 by 求知无傲      
没怎么尝试过,试试也无妨。

#3楼 [楼主]   回复  引用  查看    

2008-05-30 00:55 by jeff377      
我比較少用 TemplateField,因為我們的系統中的 GridView 需要提供每個用戶端可以自定 Columns 的順序,而 TemplateField 在動態建立或更換順序時會比較麻煩,所以會把所有會使用到 DataControlField 類型全實作出來。

#4楼    回复  引用  查看    

2008-05-30 08:22 by kiler      
@jeff377
编号列貌似无需排序吧。

#5楼 [楼主]   回复  引用  查看    

2008-05-30 09:25 by jeff377      
@kiler
我指的順序是 GridView.Columns 的欄位順序,並不是資料排序。例如預設「產品編號、產品名稱、數量、單價」,而用戶可以自訂為「數量、單價、產品編號、產品名稱」,也可以將部分的 Column 隱藏。

#6楼    回复  引用    

2008-05-30 09:26 by Amir Ai [未注册用户]
不是可以更简单吗,不用这么复杂吧

#7楼 [楼主]   回复  引用  查看    

2008-05-30 09:28 by jeff377      
@Amir Ai
你可以試著去動態產生 GridView 的 Columns,你會發覺使用 TemplateField 相當麻煩。

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: