欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

GridView添加自动编号列

发布时间:2025/5/22 编程问答 29 豆豆
生活随笔 收集整理的这篇文章主要介绍了 GridView添加自动编号列 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在开发中使用的GridView显示数据,并且需要它记录集的行编号,这样就可以很清楚的知道有多少条数据。

通过查看资料,自己做了个GridView模板,并带有分页功能示例:

Code
<asp:GridView ID="GView_Data" runat="server" AllowPaging="True" Height="1px" Font-Size="10pt" BackColor="White" BorderColor="#E7E7FF" BorderWidth="1px" OnRowDataBound="GView_Data_RowDataBound" >  
                
<Columns>
                    
<asp:TemplateField HeaderText="序号" InsertVisible="False" ControlStyle-Width="20px" >
                         
<headerstyle width="5%" horizontalalign="Center" />
                         
<ItemTemplate>
                            
<asp:Label id="Label2" runat="server" Width="100%" Text="<%# this.GView_Data.PageIndex * this.GView_Data.PageSize  + this.GView_Data.Rows.Count + 1 %>" ForeColor="Blue"></asp:Label> 
                         
</ItemTemplate>
                    
</asp:TemplateField>
                
</Columns>
                
<PagerTemplate>
                    
<table width="100%">
                        
<tr>
                            
<td style="text-align: left">
                                第
<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>'></asp:Label>
                                共
/<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount  %>'></asp:Label>
                                
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First"
                                    CommandName
="Page" Text="首页" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton>
                                
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev"
                                    CommandName
="Page" Text="上一页" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton>
                                
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next"
                                    CommandName
="Page" Text="下一页" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton>
                                
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last"
                                    CommandName
="Page" Text="尾页" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton>
                                
<asp:TextBox ID="txtNewPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>'
                                    Width
="20px"></asp:TextBox>
                                
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="Go"
                                    CommandName
="Page" Text="GO" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
                            
</td>
                        
</tr>
                    
</table>
                    
</PagerTemplate>
                    
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                    
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                    
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                    
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                    
<HeaderStyle BackColor="#7481BA" Font-Bold="True" ForeColor="Yellow" HorizontalAlign="Left" Wrap="False" />
                    
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>

 

分页功能实现方法省略。

 

总结

以上是生活随笔为你收集整理的GridView添加自动编号列的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。