c# DataGridView分页功能的实现

首先,拖两个控件BindingNavigate、BindingSource;

1.定义公共变量

        int pageCount;//总页数
        int pageRSize;//每页显示的行数
        int RowMax;//总共行数
        int currentR;//当前行号
        int currentP;//当前页号
        DataTable dat = new DataTable();

2.  获取数据

            DataBaseConnection connection = new DataBaseConnection();
            //建立数据库引擎连接,注意数据表(后缀为.db)应放在DEBUG文件下
            string sql = ("select 测线名称,起始桩号,X坐标,Y坐标,方向角,间隔,个数,线号增量,测线间距 from line where 所属项目='" + project + "'");
            //建立适配器,通过SQL语句去搜索数据库
            DataSet myds = connection.getDataSet(sql);
            dat = myds.Tables[0];
            SetIni();

3.设置

  private void SetIni(int p)
        {
            pageRSize = 10;
            if (p == 1)
            {
               
                currentP = 1;
                currentR = 0;

            }
            else
            {
                currentP = p;
                currentR = pageRSize * (currentP - 1);
            }
            RowMax = dat.Rows.Count;
            pageCount = RowMax / pageRSize;
            if (RowMax % pageRSize > 0)
            {
                pageCount++;
            }

            ncount_Label3.Text = "/" + pageCount.ToString();
            enbut();
        }

 

 private void enbut()
        {
            int nStartR = 0;   //当前页面开始行
            int nEndR = 0;     //当前页面结束行
            if (dat.Rows.Count == 0)
            {
                Describ_dataGridView.DataSource = null;
                return;
            }
            else
            {
               
                if (currentP == 1)
                {
                    bindingNavigatorMoveFirstItem.Enabled = false;
                    bindingNavigatorMovePreviousItem.Enabled = false;
                }
                else
                {
                    bindingNavigatorMoveFirstItem.Enabled = true ;
                    bindingNavigatorMovePreviousItem.Enabled = true ;
                }

                if (currentP == pageCount)
                {
                  
                    nEndR = RowMax;
                    bindingNavigatorMoveNextItem.Enabled = false;
                    bindingNavigatorMoveLastItem.Enabled = false;
                }
                else
                {
                    nEndR = pageRSize * currentP;//当前页号*每页显示的行数
                    bindingNavigatorMoveNextItem.Enabled = true ;
                    bindingNavigatorMoveLastItem.Enabled = true;
                }
                nStartR = currentR;
                bindingNavigatorPositionItem.Text = currentP.ToString();
                De_Yetxt.Text = currentP.ToString();

                getdata(nStartR, nEndR, currentR);
                
            }
        }
private void getdata(int nStartR,int nEndR,int currentR) 
        {
            DataTable dat2 = dat.Clone();//克隆DataTable结构,即将字段名称进行复制
            for (int i = nStartR; i < nEndR; i++)
            {
               
                dat2.ImportRow(dat.Rows[i]);
                currentR++;
            }
            bindingSource1.DataSource = dat2;
            bindingNavigator1.BindingSource = bindingSource1;
            Describ_dataGridView.DataSource = bindingSource1;
            Describ_dataGridView.ClearSelection();
        }

设置点击事件和输入限制

  private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if(e.ClickedItem.Text=="移到上一页")
            {
                currentP--;
                if (currentP <= 0)
                {
                    MessageBox.Show("已经是第一页了!");
                    currentP++;
                    return;
                }
                else 
                {
                   currentR=pageRSize*(currentP-1);
                }
                enbut();
            }
            if (e.ClickedItem.Text == "移到下一页")
            {
                currentP++;
                if (currentP > pageCount)
                {
                    MessageBox.Show("已经是最后一页了!");
                    currentP--;
                    return;
                }
                else
                {
                    currentR = pageRSize * (currentP - 1);
                }
                enbut();
            }
            if (e.ClickedItem.Text == "首页")
            {

                currentP = 1;

                currentR = 0;

                enbut();

            }

            if (e.ClickedItem.Text == "尾页")
            {

                currentP = pageCount;

                currentR = pageRSize * (currentP - 1);

                enbut();

            }
             if (e.ClickedItem.Text == "确定")
            {
                
                int a=Convert.ToInt32(De_Yetxt.Text.Trim());
                if (a > pageCount)
                {
                    currentP = pageCount;
                    currentR = pageRSize * (currentP - 1);
                    enbut();
                }
                else if (a < 0)
                {
                    currentP = 1;

                    currentR = 0;

                    enbut();
                }
                else
                {
                    currentP = a;
                    currentR = pageRSize * (currentP - 1);
                    enbut();
                }
            }
        }

      

        private void De_Yetxt_TextChanged(object sender, EventArgs e)
        {
            bool IsNum = true;

            foreach (char c in De_Yetxt.Text.Trim())
            {

                if (!char.IsNumber(c))
                {
                    IsNum = false;
                    break;
                }

            }

            if (IsNum == false)
            {

                De_Yetxt.Text = currentP.ToString();

            }
        }
       

平时:

如果需要在已有的的数据上增加:、

 dat.Rows.Add(new object[]{newLineName, startPile, nextLng1,nextLat1, azimuth, Interval, count});
 SetIni();

删除时

 string sq = ("select 测线名称,起始桩号,X坐标,Y坐标,方向角,间隔,个数 from line where 所属项目='" + project + "'");
            //建立适配器,通过SQL语句去搜索数据库
            DataSet myds = connection.getDataSet(sq);
            dat = myds.Tables[0];
            SetIni(Convert.ToInt32(bindingNavigatorPositionItem.Text));

 

posted @ 2015-12-28 15:46  JaneAi  阅读(1091)  评论(0编辑  收藏  举报