BindingNavigator操作DatagridView的数据

参考

http://wenku.baidu.com/link?url=NWfEfArPZvDO_aI-xEKBHVGoZY9wQO_Oty_GCsGLiPspheCzFYLf_dytuWAqN2_0AvLpK-iAun55qe7HPKjfRJ1vI8N4EHADeyQ1hNnQrdW

 

1、往窗体拉一个BindingNavigator:如图绿色框,就是一个数据导航栏

  再拉一个DataGridView,显示数据,我添加了三列,对应要显示的三列数据

  再拉一个BindingSource,作为上面两个的媒人

BindingNavigator操作DatagridView的数据

 

数据库数据如下:

BindingNavigator操作DatagridView的数据

代码如下:

 

namespace gjjyOffline
{
    public partial class fenye : Form
    {
        public fenye()
        {
            InitializeComponent();
        }

        private void fenye_Load(object sender, EventArgs e)
        {
            //加载显示数据
            using (SQLiteConnection con = new SQLiteConnection("Data Source这一串"))
            {
                con.Open();
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = string.Format(@"select * from jy_dic_crop");//要显示的数据
                    int rows = cmd.ExecuteNonQuery();
                    SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    //con.Close();
                    DataTable dtbl = ds.Tables[0];
                    dataGridView1.AutoGenerateColumns = false;
                    this.dataGridView1.DataSource = dtbl;
            //绑定每列的值显示在DatagridView
                    this.dataGridView1.Columns["column1"].DataPropertyName = dtbl.Columns["id"].ToString();//column1是DatagridView的第一列的name值
                    this.dataGridView1.Columns["column2"].DataPropertyName = dtbl.Columns["name"].ToString();
                    this.dataGridView1.Columns["column3"].DataPropertyName = dtbl.Columns["status"].ToString();
            //将DatagridView的数据通过BindingSource与BindingNavigator连接起来 BindingSource bs
= new BindingSource(); bs.DataSource = dtbl; bindingNavigator1.BindingSource = bs; dataGridView1.DataSource = bs; } } } } }

 

效果如下:

DatagridView的数据与BindingNavigator导航栏联系起来了

 BindingNavigator操作DatagridView的数据

 

分页的实现:

重新编辑BindingNavigator

BindingNavigator操作DatagridView的数据

 在上面代码的基础上,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;


namespace gjjyOffline
{
    public partial class fenye : Form
    {
        public fenye()
        {
            InitializeComponent();
        }

        int pageSize = 0;//页面行数
        int total = 0;
        int pageCount = 0;//总页数

        int pageCurrent = 0;
        int currentRow = 0;//当前记录数从0开始
        
        int nStartPos = 0;
        int nEndPos = 0;

        DataTable dtbl = null;

        private void LoadData()
        {
            nStartPos = 0;
            nEndPos = 0;
            DataTable dtTemp = dtbl.Clone();
            if (pageCurrent == pageCount)
            {
                nEndPos = total;
            }
            else
            {
                nEndPos = pageSize * pageCurrent;
            }
            nStartPos = currentRow;

            toolStripLabel2.Text = "/" + pageCount.ToString();
            if (dtbl.Rows.Count == 0)
            {
                toolStripTextBox1.Text = "0";
            }
            else
            {
                toolStripTextBox1.Text = Convert.ToString(pageCurrent);
            }
            this.label2.Text = total.ToString();
            //从元数据源复制记录行
            if (dtbl.Rows.Count != 0)
            {
                for (int i = nStartPos; i < nEndPos; i++)
                {
                    dtTemp.ImportRow(dtbl.Rows[i]);
                    currentRow++;
                }

            }
            bindingSource1.DataSource = dtTemp;
            bindingNavigator1.BindingSource = bindingSource1;
            dataGridView1.DataSource = bindingSource1;

        }
        
        private void fenye_Load(object sender, EventArgs e)
        {
            //加载显示数据
            using (SQLiteConnection con = new SQLiteConnection("Data Source这一串"))
            {
                con.Open();
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = string.Format(@"select * from jy_dic_crop");
                    int rows = cmd.ExecuteNonQuery();
                    SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    //con.Close();
                     dtbl = ds.Tables[0];
                    dataGridView1.AutoGenerateColumns = false;
                    this.dataGridView1.DataSource = dtbl;

                    this.dataGridView1.Columns["column1"].DataPropertyName = dtbl.Columns["id"].ToString();
                    this.dataGridView1.Columns["column2"].DataPropertyName = dtbl.Columns["name"].ToString();
                    this.dataGridView1.Columns["column3"].DataPropertyName = dtbl.Columns["status"].ToString();

                    BindingSource bs = new BindingSource();
                    bs.DataSource = dtbl;
                    bindingNavigator1.BindingSource = bs;
                    dataGridView1.DataSource = bs;

                     pageSize = 10;
                     total = dtbl.Rows.Count;
                     pageCount=(total/pageSize);
                    if((total%pageSize>0))
                    {
                        pageCount++;
                    }
                     pageCurrent = 1;
                     currentRow = 0;//当前记录数从0开始

                     LoadData();       
                }
            }    
        }

        private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if(e.ClickedItem.Text=="上一页")
            {
                if(pageCurrent>=0)
                {
                    pageCurrent--;
                }
                if (pageCurrent <= 0)
                {
                    pageCurrent++;
                    MessageBox.Show("已经是第一页");
                    return;
                }
                else
                {
                    currentRow=pageSize*(pageCurrent-1);
                }
                //
                LoadData();
                //


            }

            if(e.ClickedItem.Text=="下一页")
            {
                if(pageCurrent<=pageCount)
                {
                    pageCurrent++;
                }
                if (pageCurrent > pageCount)
                {
                    pageCurrent--;
                    MessageBox.Show("已经是最后一页");
                    return;
                }
                else
                {
                    currentRow=pageSize*(pageCurrent-1);
                }
                //
                nStartPos = 0;
                nEndPos = 0;
                DataTable dtTemp = dtbl.Clone();
                if (pageCurrent == pageCount)
                {
                    nEndPos = total;
                }
                else
                {
                    nEndPos = pageSize * pageCurrent;
                }
                nStartPos = currentRow;

                toolStripLabel2.Text = "/" + pageCount.ToString();
                if (dtbl.Rows.Count == 0)
                {
                    toolStripTextBox1.Text = "0";
                }
                else
                {
                    toolStripTextBox1.Text = Convert.ToString(pageCurrent);
                }
                this.label2.Text = total.ToString();
                //从元数据源复制记录行
                if (dtbl.Rows.Count != 0)
                {
                    for (int i = nStartPos; i < nEndPos; i++)
                    {
                        dtTemp.ImportRow(dtbl.Rows[i]);
                        currentRow++;
                    }
                }
                bindingSource1.DataSource = dtTemp;
                bindingNavigator1.BindingSource = bindingSource1;
                dataGridView1.DataSource = bindingSource1;
                //
            }
        }
    }
}