DataGrid自定义导航按钮

本文介绍如何在Visual Studio 2003中使用DataGrid控件,并自定义其分页导航功能,通过添加前后页及跳转按钮实现更灵活的页面导航。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VS中的DataGrid相信大家都很熟悉了, 功能十分的强大,唯一觉得有点遗憾的是分页导航,效果不是十分的完美,所以自己写了个自定义导航按钮。代码如下:

(数据表引用数据库Northwind中的products表。)

VS2003

DataGridNavig.aspx

    <body MS_POSITIONING="GridLayout">         <form id="Form1" method="post" runat="server">             <table>                 <tr>                     <td>                         <asp:DataGrid id="myDataGrid" runat="server" Width="320px" Height="184px" AllowSorting="True"                             ShowFooter="True" PageSize="5" AllowPaging="True" BorderColor="#E7E7FF" BorderStyle="None"                             BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Horizontal">                             <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>                             <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>                             <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>                             <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>                             <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>                             <Columns>                                 <asp:TemplateColumn HeaderText="ID">                                     <ItemTemplate>                                         <%#DataBinder.Eval(Container.DataItem,"ProductID")%>                                     </ItemTemplate>                                 </asp:TemplateColumn>                                 <asp:TemplateColumn HeaderText="ProductName">                                     <ItemTemplate>                                         <%#DataBinder.Eval(Container.DataItem,"ProductName")%>                                     </ItemTemplate>                                 </asp:TemplateColumn>                                 <asp:TemplateColumn HeaderText="UnitPrice">                                     <ItemTemplate>                                         <%#DataBinder.Eval(Container.DataItem,"UnitPrice","{0:C}")%>                                     </ItemTemplate>                                 </asp:TemplateColumn>                             </Columns>                             <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>                         </asp:DataGrid>                     </td>                 </tr>                 <tr>                     <td align="right">                         共<%=this.myDataGrid.PageCount - 1%>                         页 第<%=this.myDataGrid.CurrentPageIndex + 1%>                        <asp:LinkButton ID="btnFirst" Runat="server"></asp:LinkButton>                         <asp:LinkButton ID="btnNext" Runat="server"></asp:LinkButton>                         <asp:LinkButton ID="btnPrev" Runat="server"></asp:LinkButton>                         <asp:LinkButton ID="btnLast" Runat="server"></asp:LinkButton>                         转                         <asp:TextBox ID="txtPageIndex" Runat="server" Width="32px"></asp:TextBox>                         页                         <asp:Button ID="btnRect" Text="" Runat="server" Width="24px"></asp:Button>                     </td>                 </tr>             </table>         </form>     </body>

DataGridNavig.aspx.cs

private void Page_Load(object sender, System.EventArgs e) {     if(!IsPostBack)     {         this.GridBund();//绑定数据         this.btnFirst.Text= "首页";         this.btnNext.Text= "下一页";         this.btnPrev.Text = "上一页";         this.btnLast.Text = "最后页";     }          } private void  GridBund() {     string stringSql = "select productID,productName,UnitPrice from products";     DataSet ds = SQLHelper.ExecuteDataSet(stringSql);     this.myDataGrid.DataSource = ds.Tables[0].DefaultView;     this.myDataGrid.DataBind(); } /// <summary> /// 分页 /// </summary> private void myDataGrid_PageIndexChanged(object source,DataGridPageChangedEventArgs e) {     try     {         this.myDataGrid.CurrentPageIndex = e.NewPageIndex;     }     catch     {         this.myDataGrid.CurrentPageIndex = 0;     }     finally     {         this.GridBund();     } } /// <summary> /// 首页 /// </summary> private void btnFirst_Click(object sender, System.EventArgs e) {     this.myDataGrid.CurrentPageIndex = 0;     this.GridBund(); } /// <summary> /// 下一页 /// </summary> private void btnNext_Click(object sender, System.EventArgs e) {     if(this.myDataGrid.CurrentPageIndex < (this.myDataGrid.PageCount - 1))     {         this.myDataGrid.CurrentPageIndex += 1;         this.GridBund();     } } /// <summary> /// 上一页 /// </summary> private void btnPrev_Click(object sender, System.EventArgs e) {     if(this.myDataGrid.CurrentPageIndex > 0)     {         this.myDataGrid.CurrentPageIndex -= 1;         this.GridBund();     } } /// <summary> /// 最后页 /// </summary>          private void btnLast_Click(object sender, System.EventArgs e) {     this.myDataGrid.CurrentPageIndex = (this.myDataGrid.PageCount -1);     this.GridBund(); } /// <summary> /// 转到第几页 /// </summary> private void btnRect_Click(object sender, System.EventArgs e) {     int num = Int32.Parse(this.txtPageIndex.Text);     if(num <= 0 || num > (this.myDataGrid.PageCount -1))         return;     else     {         this.myDataGrid.CurrentPageIndex = (num - 1);         this.GridBund();     } }

 如果有更好的方法,大家一起交流!^ ^

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值