• C#实现DataGridView分页功能


    从界面可以看到,在设计时需要一个DataGridView、BindingNavigate、BindingSource控件,分别命名为dgvInfo、bdnInfo、bdsInfo。
         在bdnInfo控件中添加几个用于选择页面的lable和botton,如上图所示。
         设计时:
         1、定义几个所需的公有成员:
            int pageSize = 0;     //每页显示行数
            int nMax = 0;         //总记录数
            int pageCount = 0;    //页数=总记录数/每页显示行数
            int pageCurrent = 0;   //当前页号
            int nCurrent = 0;      //当前记录行
            DataSet ds = new DataSet();
            DataTable dtInfo = new DataTable();
        2、在窗体载入事件中,从数据源读取记录到DataTable中:
                string strConn = "SERVER=127.0.0.1;DATABASE=NORTHWIND;UID=SA;PWD=ULTRATEL";   //数据库连接字符串
                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();
                string strSql = "SELECT * FROM CUSTOMERS";
                SqlDataAdapter sda = new SqlDataAdapter(strSql,conn);
                sda.Fill(ds,"ds");
                conn.Close();
                dtInfo = ds.Tables[0];
                InitDataSet();
          3、用当前页面数据填充DataGridView
    private void InitDataSet()
            ...{
                pageSize = 20;      //设置页面行数
                nMax = dtInfo.Rows.Count;
                pageCount=(nMax/pageSize);    //计算出总页数
                if ((nMax % pageSize) > 0) pageCount++;
                pageCurrent = 1;    //当前页数从1开始
                nCurrent = 0;       //当前记录数从0开始
                LoadData();
            }
    private void LoadData()
            ...{
                int nStartPos = 0;   //当前页面开始记录行
                int nEndPos = 0;     //当前页面结束记录行
                DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架
                if (pageCurrent == pageCount)
                    nEndPos = nMax;
                else
                    nEndPos = pageSize * pageCurrent;
                nStartPos = nCurrent;
                lblPageCount.Text = pageCount.ToString();
                txtCurrentPage.Text = Convert.ToString(pageCurrent);
                //从元数据源复制记录行
                for (int i = nStartPos; i < nEndPos; i++)
                ...{
                    dtTemp.ImportRow(dtInfo.Rows[i]);
                    nCurrent++;
                }

                bdsInfo.DataSource = dtTemp;
                bdnInfo.BindingSource = bdsInfo;
                dgvInfo.DataSource = bdsInfo;
            }
            4、菜单响应事件:
    private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            ...{
                if (e.ClickedItem.Text == "关闭")
                ...{
                    this.Close();
                }
                if (e.ClickedItem.Text == "上一页")

                ...{
                    pageCurrent--;
                    if (pageCurrent <= 0)
                    ...{
                        MessageBox.Show("已经是第一页,请点击“下一页”查看!");
                        return;
                    }
                    else
                    ...{

                        nCurrent = pageSize * (pageCurrent - 1);
                    }
                    LoadData();
                }
                if (e.ClickedItem.Text == "下一页")
                ...{
                    pageCurrent++;
                    if (pageCurrent > pageCount)
                    ...{
                        MessageBox.Show("已经是最后一页,请点击“上一页”查看!");

    软件开发网 www.mscto.cn

                        return;
                    }
                    else
                    ...{
                       nCurrent=pageSize*(pageCurrent-1);
                    }
                    LoadData();
                }
            }

  • 相关阅读:
    移动安全
    Photoshop笔记
    ARMv7 与 ARMv8对比
    centos系统 网络配置
    视频大文件压缩
    开发者必备Linux命令
    开发者必备Docker命令
    文件服务器minio
    Java 图片Base64
    socket
  • 原文地址:https://www.cnblogs.com/googlegis/p/2978990.html
Copyright © 2020-2023  润新知