如何在DataGrid中实现分页功能

效果 :

DataGrid分页的例子

lastname firstname
Davolio Nancy
Fuller Andrew
Leverling Janet
Peacock Margaret
Buchanan Steven
1 2
最首页前一页下一页最后页

代码:

     private   void  ShowStats()
        
{
            
//显示当前页面是第几页
            lblCurrentIndex.Text = "第 " + (DataGridPage.CurrentPageIndex + 1).ToString() + " 页";
            
//显示总页数
            lblPageCount.Text = "总共 " + DataGridPage.PageCount.ToString() + " 页";
        }


     public   void  PageButtonClick( object  sender, EventArgs e)
        
{
            
//取得按钮单击时传递的命令参数
            string arg = ((LinkButton)sender).CommandArgument.ToString();
            
switch(arg)
            
{
                
//如果点击的是“下一页”
                case "next":
                    
//如果当前页不是最后一页
                    if (DataGridPage.CurrentPageIndex < (DataGridPage.PageCount - 1))
                    
{
                        
//设置DataGrid控件的当前页索引为下一页面
                        DataGridPage.CurrentPageIndex += 1;
                    }

                    
break;
                
//如果点击的是“前一页”
                case "prev":
                    
//如果当前页不是首页
                    if (DataGridPage.CurrentPageIndex > 0)
                    
{
                        
//设置DataGrid控件的当前页索引为上一页面
                        DataGridPage.CurrentPageIndex -= 1;
                    }

                    
break;
                
//如果点击的是“最后页”
                case "last":
                    
//设置当前页的索引为最后一页
                    DataGridPage.CurrentPageIndex = (DataGridPage.PageCount - 1);
                    
break;
                
//默认为”最首页“
                default:
                    
//设置当前页的索引为首页
                    DataGridPage.CurrentPageIndex = System.Convert.ToInt32(arg);
                    
break;
            }

            BindGrid();
            ShowStats();
        }


         public   void  DataGridPage_Page( object  sender, DataGridPageChangedEventArgs e)
        
{
            
//设置DataGrid当前页的索引值为用户选择的页的索引
            DataGridPage.CurrentPageIndex = e.NewPageIndex;
            
//重新绑定数据
            BindGrid();
            
//显示当前分页信息
            ShowStats();
        }


     private   void  InitializeComponent()
        
{    
            
this.DataGridPage.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGridPage_Page);
            
this.DataGridPage.SelectedIndexChanged += new System.EventHandler(this.DataGridPage_SelectedIndexChanged);
            
this.Load += new System.EventHandler(this.Page_Load);

        }

你可能感兴趣的:(datagrid)