zoukankan      html  css  js  c++  java
  • GridView 72般绝技之与Linq喜结连理 之1,GridView自动分页

    看了园子里的文章,有感而发,用Linq去实现。
    http://blog.csdn.net/lipan2/archive/2008/09/14/2795707.aspx
    此节主要关注Linq的分页,源代码在后边附上(喜欢附上源代码)

    客服端代码:

    代码
            <asp:GridView ID="gridViewCourse" runat="server" AllowPaging="True" 
                AllowSorting
    ="True" AutoGenerateColumns="False" 
                PageSize
    ="5" Width="614px" 
                onpageindexchanged
    ="gridViewCourse_PageIndexChanged" 
                onpageindexchanging
    ="gridViewCourse_PageIndexChanging">
                
    <Columns>
                    
    <asp:BoundField DataField="Cno" HeaderText="学号" />
                    
    <asp:BoundField DataField="Name" HeaderText="姓名" />
                    
    <asp:BoundField DataField="Time" HeaderText="入学时间" />
                    
    <asp:BoundField DataField="Money" HeaderText="" />
                
    </Columns>
            
    </asp:GridView

    设置着三个属性即可:
    AllowPaging="True"
    AllowSorting="True" AutoGenerateColumns="False"
    PageSize="5"
    服务端代码:

    代码
     public partial class GridNoCodePageForm : System.Web.UI.Page
        {
            
    private CourserService courseServcie;
            
    private int currentIndex = 1;
            
    private int pageSize = 5;

            
    protected void Page_Load(object sender, EventArgs e)
            {
                
    if (!IsPostBack)
                {
                    courseServcie 
    = new CourserService();
                    List
    <Course> courseList = courseServcie.FindAllList();
                    
    if (courseList.Count == 0)
                        MessageBox.Show(
    "没有数据了!");
                    
    this.gridViewCourse.DataSource = courseList;
                    
    this.gridViewCourse.DataBind();
                }
            }

            
    private void BindSource(int pageIndex)
            {
                courseServcie 
    = new CourserService();
                List
    <Course> courseList = courseServcie.FindList(pageIndex, pageSize);
                
    if (courseList.Count == 0)
                    MessageBox.Show(
    "没有数据了!");
                
    this.gridViewCourse.DataSource = courseList;
                
    this.gridViewCourse.DataBind();
            }

            
    protected void gridViewCourse_PageIndexChanged(object sender, EventArgs e)
            {
            }

            
    protected void gridViewCourse_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                
    int currentIndex = e.NewPageIndex;
                BindSource(currentIndex);
            }


    实现数据的绑定:以及在点击“2”数字的时候处理的代码。这种绑定数据的方法不好之处在于,只要绑定的数据大于5条时,才显示
    有数字页。而且,Linq 的分页也同样有问题。它是查询出来所有的学生信息,然后再用Skip和Take方法实现分页的。急切需要网友斧正。

    代码
            public List<Course> FindList(int pageIndex, int pageSize)
            {
                pageIndex 
    = pageIndex * pageSize - pageSize;
                List
    <Course> objList = new List<Course>();
                Table
    <Course> couse = context.GetTable<Course>();
                var courseQuery 
    =
                    from couseItem 
    in couse
                    orderby couseItem.Cno
                    select couseItem;
                var pageQuery 
    = courseQuery.Skip<Course>(pageIndex).Take<Course>(pageSize);
                
    foreach (Course cou in pageQuery)
                {
                    objList.Add(cou);
                }
                
    return objList;



  • 相关阅读:
    STM32学习之路-SysTick的应用(时间延迟)
    STM32M CUBE实现printf打印调试信息以及实现单字节接收
    iframe动态创建及释放内存
    第13周项目2-成绩处理
    1036. Boys vs Girls (25)
    CS0433: 类型“BasePage”同一时候存在于“c:WindowsMicrosoft.NETxxxxxxxxxxxxxxxx
    Java读取Excel转换成JSON字符串进而转换成Java对象
    Java对象与JSON互相转换jsonlib以及手动创建JSON对象与数组——(二)
    GSON中Java对象与JSON互相转换——(一)
    Java泛型方法与泛型类的使用------------(五)
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1712264.html
Copyright © 2011-2022 走看看