C# 利用Datagridview、BindingNavigator、BindingSource自動顯示資料

蘑菇色的小象發表於2018-12-13

DataGridview是要進行顯示的控制元件

BindingNavigator是導航控制元件,可以對Datagridview中的資料進行操作

BindingSource是資料來源,用來繫結資料庫或者表格

下邊是影像化介面

上邊的導航鍵從紅色X以前的按鍵無需進行編寫程式碼,之後的要進行編寫程式碼進行實現,後邊的標籤、多選框、文字框以及按鈕都是從黃色+號衍生過來的,點選加號即可自行新增想要的東西。至於上邊導航鍵中的屬性名稱,可以自行對應下邊的程式碼中,親測有效。

        /// <summary>
        /// 系統列表導航鍵
        /// </summary>
        #region 
        /// <summary>
        /// 對?/總頁數進行賦值(?)
        /// </summary>
        private void ComboBoxValue()
        {
            for (int i = 1; i < _pageCount + 1; i++)
            {
                txtCurrentPage.Items.Add(i);
            }
        }

        /// <summary>
        /// 每頁顯示記錄數
        /// </summary>
        private int _pageSize = 100;
        /// <summary>
        /// 每頁顯示記錄數
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }/// <summary>
         /// 總記錄數
         /// </summary>
        private int _nMax = 0;
        /// <summary>
        /// 總記錄數
        /// </summary>
        public int NMax
        {
            get { return _nMax; }
            set { _nMax = value; }
        }/// <summary>
         /// 頁數=總記錄數/每頁顯示記錄數
         /// </summary>
        private int _pageCount = 0;
        /// <summary>
        /// 頁數=總記錄數/每頁顯示記錄數
        /// </summary>
        public int PageCount
        {
            get { return _pageCount; }
            set { _pageCount = value; }
        }/// <summary>
         /// 當前頁號
         /// </summary>
        private int _pageCurrent = 0;
        /// <summary>
        /// 當前頁號
        /// </summary>
        public int PageCurrent
        {
            get { return _pageCurrent; }
            set { _pageCurrent = value; }
        }/// <summary>
         /// 當前記錄行
         /// </summary>
        private int _nCurrent = 0;
        /// <summary>
        /// 當前記錄行
        /// </summary>
        public int NCurrent
        {
            get { return _nCurrent; }
            set { _nCurrent = value; }
        }
        /// <summary>
        /// 設定資料來源
        /// </summary>
        private DataTable dataSource = null;
        /// <summary>
        /// 設定資料來源
        /// </summary>
        public DataTable DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }
        //private DataSet ds = null;

        public void InitDataSet(DataTable dt)
        {
            this.dataSource = dt;
            this._nMax = this.dataSource.Rows.Count;
            //計算總頁數
            this._pageCount = _nMax / _pageSize;
            //
            if ((_nMax % _pageSize) > 0)
                _pageCount++;
            _pageCurrent = 1;
            _nCurrent = 0;
            FillData();
        }


        /// <summary>
        /// 填充資料
        /// </summary>
        private void FillData()
        {
            //當前頁面開始記錄行
            int nStartPos = 0;
            //當前頁面結束記錄行
            int nEndPos = 0;
            DataTable dtTemp = this.dataSource.Clone();
            if (this._pageCurrent == this._pageCount)
                nEndPos = _nMax;
            else
                nEndPos = this._pageCurrent * this._pageSize;

            nStartPos = this._nCurrent;

            lblMaxPage.Text = this._pageCount.ToString();//頁數
            txtCurrentPage.Text = this._pageCurrent.ToString();//當前頁

            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(this.dataSource.Rows[i]);
                _nCurrent++;
            }
            bindingSource1.DataSource = dtTemp;//繫結資料來源,管控TMSI表格
            dgvSoundResult.DataSource = bindingSource1;
            bindingNavigator1.BindingSource = bindingSource1;

        }
        /// <summary>
        /// 導航鍵
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>  
        private void bindingNavigator1_ItemClicked_1(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text)
            {
                case "移到上一條記錄":
                    //FillData();
                    break;
                case "移到下一條記錄":
                    //FillData();
                    break;
                case "上一頁":
                    lblMsg1.Enabled = true;
                    if (this._pageCurrent == 1)
                    {
                        MessageBox.Show("已經是第一頁,請點選“下一頁”檢視!");
                        // lblMsg.Text = "已經是第一頁,請點選“下一頁”檢視!";
                        return;
                    }
                    else
                    {
                        _pageCurrent--;
                        this._nCurrent = this._pageSize * (_pageCurrent - 1);
                        FillData();
                    }

                    break;
                case "下一頁":
                    lblMsg.Enabled = true;

                    if (this._pageCurrent == _pageCount)
                    {
                        lblMsg1.Enabled = false;
                        MessageBox.Show("已經是最後一頁,請點選“上一頁”檢視!");
                        //lblMsg1.Text = "已經是最後一頁,請點選“上一頁”檢視!";
                        return;
                    }
                    else
                    {
                        _pageCurrent++;
                        this._nCurrent = this._pageSize * (_pageCurrent - 1);
                        FillData();
                    }

                    break;
            }
        }
        /// <summary>
        /// 跳轉
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (int.TryParse(txtCurrentPage.Text.Trim(), out num) && num > 0)
            {
                _nCurrent = (num - 1) * 100;
                _pageCurrent = num;
                FillData();
            }
        }
        /// <summary>
        /// 跳轉頁數限制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtCurrentPage_TextChanged_1(object sender, EventArgs e)
        {
            int num = 0;
            if (int.TryParse(txtCurrentPage.Text.Trim(), out num) && num > 0)
            {
                if (num > _pageCount)
                {
                    txtCurrentPage.Text = _pageCount.ToString();
                }
            }
        }
        #endregion

      在想要進行顯示時,只需要呼叫 public void InitDataSet(DataTable dt)這個方法即可,裡邊的引數是一張表,如果這張表是從資料庫取出來的,則會省掉很多事。如果沒有資料庫,則要先在本地建立一張表,顯示的欄位初始化時要載入到datagridview中,這種從資料來源取資料進行顯示,不允許datagridview剛開始就把欄位寫死在首行,欄位名是自行動態載入進來的,而且過程中不允許datagridview在程式碼中自動新增行,只能重新整理資料表取資料。所以假如想要剛進入系統的時候,就看到欄位顯示在列表中,就必須初始化datagridview,程式碼如下,只是上述程式碼中的一部分即可實現功能。

        /// <summary>
        /// 系統列表最開始進行初始化
        /// </summary>
        /// <param name="dt"></param>
        #region 
        public void InitData(DataTable dt)
        {
            this.dataSource = dt;
            this._nMax = this.dataSource.Rows.Count;
            //計算總頁數
            this._pageCount = _nMax / _pageSize;
            //
            if ((_nMax % _pageSize) > 0)
                _pageCount++;
            _pageCurrent = 1;
            _nCurrent = 0;
            InitalFillData();
        }
        /// <summary>
        /// 填充資料
        /// </summary>
        private void InitalFillData()
        {
            //當前頁面開始記錄行
            int nStartPos = 0;
            //當前頁面結束記錄行
            int nEndPos = 0;
            DataTable dtTemp = this.dataSource.Clone();
            if (this._pageCurrent == this._pageCount)
                nEndPos = _nMax;
            else
                nEndPos = this._pageCurrent * this._pageSize;

            nStartPos = this._nCurrent;

            lblMaxPage.Text = this._pageCount.ToString();//頁數
            txtCurrentPage.Text = this._pageCurrent.ToString();//當前頁
            bindingSource1.DataSource = dtTemp;//
            dgvSoundResult.DataSource = bindingSource1;
            bindingNavigator1.BindingSource = bindingSource1;
        }

關於繫結資料來源顯示列表就先說到這裡,歡迎大家批評指正。

相關文章