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>
<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>
分页功能实现方法省略。
作者:zeke
出处:http://zhf.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。