winfrom datagridview分页显示

一、概述

datagridview绑定数据并分页操作,因为用到了webservice,所以代码会详细讲解。QueryByCondition是一个查询函数。

二、客户端

PageData pageData=new PageData();//用来接收数据
public
bool QueryByCondition(Entity.ProductStatus status, int pageIndex) {
       SoapHeaderTransferData();//webservice 的soap身份验证,它的作用能够访问webservice服务端,如果你没有使用webservice,则不用加。
string query_condition = txt_title.Text.Trim().ToString();//查询条件
        //返回string类型,序列化,其实是类实体转化为string,方便在webservice之间传输。
string str = ps.QueryByCondition(query_condition, (pm.ProductStatus)Enum.Parse(typeof(pm.ProductStatus),status.ToString()),
             user_id, pageIndex, pageSize);//有五个参数,前三个是查询条件,后两个是分页条件,pageIndex是当前页根据自己的需要输入, pageSize是一页有多少条数据,也是根据需要输入
if (str == null) { return false; } else {
          //将str反序列化,获得类实体数据 pageData
= VCommons.ObjectXmlExtensions.ToObj<Entity.Ot.PageData>(VCommons.Utils.UrlDecode(str)); this.DataGridViewProduct.AutoGenerateColumns = false;//设置datagridview不能够自动添加列 this.DataGridViewProduct.DataSource = pageData.Data; DataGridViewProduct.ClearSelection(); lblPageCount.Text = pageData.TotalPage.ToString(); lbCurrentPage.Text = pageData.PageIndex.ToString(); return true;
} }

三、服务端

#region 按条件查询产品信息
        [SoapHeader("myHeader")]
        [WebMethod(EnableSession = true)]
        public string QueryByCondition(string query_condition, Entity.ProductStatus status, string userid, int pageIndex, int pageSize)
        {
      //进行身份验证
if (myHeader.CheckLogin()) { using (var repository = new DataE.VAERP.Repository()) {
            //根据条件查询数据
var linq = from product in repository.GetIQueryable<Entity.VAERP.Product>() join data in repository.GetIQueryable<Entity.VAERP.ProductData>() on product.ID equals data.ProductID where (product.UserID == userid || data.SellerID == userid) && product.Status == status select product; IQueryable<Entity.VAERP.Product> pros = string.IsNullOrWhiteSpace(query_condition) ? linq : linq.Where(item => item.Title.StartsWith(query_condition) || item.Title.EndsWith(query_condition) || item.Title.IndexOf(query_condition) != -1);
              //进行分页操作
             var pageData = new Entity.PagedList<Entity.VAERP.Product>(pros, pageIndex, pageSize); //序列化 var str = VCommons.Utils.UrlEncode(new Entity.Ot.PageData() { Data = pageData.ToArray(), PageIndex = pageData.PageIndex, PageSize = pageData.PageSize, TotalPage = pageData.TotalPages }.ToXml()); return str; } } else { return VCommons.Utils.UrlEncode(new Entity.PagedList<Entity.VAERP.Product>(null, pageIndex, pageSize).ToXml()); } } #endregion

四、分页方法PagedList

 public PagedList(IQueryable<T> source, int index, int pageSize)
        {
            if (source != null) //判断传过来的实体集是否为空
            {
                int total = source.Count();
                this.TotalCount = total;
                this.TotalPages = total / pageSize;

                if (total % pageSize > 0)
                    TotalPages++;

                this.PageSize = pageSize;
                if (index > this.TotalPages)
                {
                    index = this.TotalPages;
                }
                if (index < 1)
                {
                    index = 1;
                }
                this.PageIndex = index;
                this.AddRange(source.Skip((index - 1) * pageSize).Take(pageSize).ToList()); //Skip是跳到第几页,Take返回多少条
            }
        }
原文地址:https://www.cnblogs.com/qtiger/p/5629098.html