关闭

DataGrid自定义导航按钮

标签: datagridaspobjectservertextboxtable
405人阅读 评论(1) 收藏 举报
分类:

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();     } }

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3456次
    • 积分:69
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论