BindingNavigator操作DatagridView的數據


參考

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

 

1、往窗體拉一個BindingNavigator:如圖綠色框,就是一個數據導航欄

  再拉一個DataGridView,顯示數據,我添加了三列,對應要顯示的三列數據

  再拉一個BindingSource,作為上面兩個的媒人

 

數據庫數據如下:

代碼如下:

 

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

 在上面代碼的基礎上,

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;
                //
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM