前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BindingNavigator操作DatagridView的数据[通俗易懂]

BindingNavigator操作DatagridView的数据[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-14 16:06:53
9860
发布2022-09-14 16:06:53
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

参考

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

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

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

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

数据库数据如下:

代码如下:

代码语言:javascript
复制
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

在上面代码的基础上,

代码语言:javascript
复制
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;
                //
            }
        }
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158281.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档