articles

Home / DeveloperSection / Articles / Implement Paging in Datagridview C#

Implement Paging in Datagridview C#

Vijay Shukla 36947 17-Jun-2013
Paging

Paging is a concept which is helpful for our data management; in this article I am going to explain a demo which shows how to implement paging in datagridview.

Using the code:-

1.    Create a new project in VS Studio 2010.

2.   Drag a datagridview from toolbox and drop on form, added datagridview with name dgvPaging.

3.   Drag toolstrip from toolbox and drop on form (I’m renaming toolstrip with toolStripPaging).

4.   Right click on toolStripPaging and select edit option, after selecting edit open a dialog box where we add buttons.

Implement Paging in Datagridview C#

5.   Click on OK.

6.   Now after that get all button Click event.

Form1.cs code: -
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DatagridviewPaging;

namespace DatagridviewPaging
{
    public partial class formPaging : Form
    {
        public formPaging()
        {
            InitializeComponent();
        }
        private int ActivePage = 1;//Here give the by default selected page in datagridview
        int PagesCounter = 1; //Here give the by default count page in datagridview
        int DefaultShowRows = 5; //Here we give how many data shows in datagridview
        BindingList<listData> Baselist = null;
        BindingList<listData> Templist = null;
        private void RebindGridForPageChange()
        {
            //Rebinding the Datagridview with data
            int datasourcestartIndex = (ActivePage - 1) * DefaultShowRows;
            Templist = new BindingList<listData>();
            for (int i = datasourcestartIndex; i < datasourcestartIndex + DefaultShowRows; i++)
            {
                if (i >= Baselist.Count)
                    break;

                Templist.Add(Baselist[i]);
            }

            dgvPaging.DataSource = Templist;
            dgvWidth();


        }
        private void dgvWidth()
        {
            dgvPaging.Columns[0].Width = 200;
            dgvPaging.Columns[1].Width = 100;
            dgvPaging.Columns[0].HeaderText = "Student Name";
            dgvPaging.Columns[1].HeaderText = "Student Class";

        }
        private void RefreshPagination()
        {
            ToolStripButton[] items = new ToolStripButton[] { toolStripButton1, toolStripButton2, toolStripButton3, toolStripButton4, toolStripButton5 };
            //pageStartIndex contains the first button number of pagination.
            int pageStartIndex = 1;

            if (PagesCounter > 5 && ActivePage > 2)
                pageStartIndex = ActivePage - 2;

            if (PagesCounter > 5 && ActivePage > PagesCounter - 2)
                pageStartIndex = PagesCounter - 4;

            for (int i = pageStartIndex; i < pageStartIndex + 5; i++)
            {
                if (i > PagesCounter)
                {
                    items[i - pageStartIndex].Visible = false;
                }
                else
                {
                    //Changing the page numbers
                    items[i - pageStartIndex].Text = i.ToString(System.Globalization.CultureInfo.InvariantCulture);

                    //Setting the Appearance of the page number buttons
                    if (i == ActivePage)
                    {
                        items[i - pageStartIndex].BackColor = System.Drawing.ColorTranslator.FromHtml("#83D6F6");
                        items[i - pageStartIndex].ForeColor = Color.White;
                    }
                    else
                    {
                        items[i - pageStartIndex].BackColor = Color.White;
                        items[i - pageStartIndex].ForeColor = System.Drawing.ColorTranslator.FromHtml("#83D6F6");
                    }
                }
            }

            //Enabling or Disalbing pagination first, last, previous , next buttons
            if (ActivePage == 1)
                btnBackward.Enabled = btnFirst.Enabled = false;
            else
                btnBackward.Enabled = btnFirst.Enabled = true;

            if (ActivePage == PagesCounter)
                btnForward.Enabled = btnLast.Enabled = false;

            else
                btnForward.Enabled = btnLast.Enabled = true;
        }
        DataTable _dataTable = new DataTable();
        private BindingList<listData> FillDataforGrid()
        {
            BindingList<listData> list = new BindingList<listData>();
            for (int i = 0; i < _dataTable.Rows.Count; i++)
            {
                listData obj = new listData(_dataTable.Rows[i][0].ToString(), _dataTable.Rows[i][1].ToString());
                list.Add(obj);
            }

            return list;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                _dataTable.Columns.Add("Student Name", typeof(string));
                _dataTable.Columns.Add("Student Class", typeof(string));
                for (int i = 0; i < 50; i++)
                {
                    //dgvPaging.Rows.Add("Student Name" + i, "Student Class" + i);
                    _dataTable.Rows.Add("Student Name" + i, "Student Class" + i);
                }
                Baselist = FillDataforGrid();
                PagesCounter = Convert.ToInt32(Math.Ceiling(Baselist.Count * 1.0 / DefaultShowRows));
                RefreshPagination();
                RebindGridForPageChange();
            }
            catch (Exception ex)
            {
            }
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();
            }
            catch (Exception) { }
        }

        private void btnBackward_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();
            }
            catch (Exception) { }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();

            }
            catch (Exception) { }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();
            }
            catch (Exception) { }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();
            }
            catch (Exception) { }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();
            }
            catch (Exception) { }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();
            }
            catch (Exception) { }
        }


        private void btnForward_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();
            }
            catch (Exception) { }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);
                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();
            }
            catch (Exception) { }
        }

        private void btnFirst_Click_1(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

                //Determining the current page
                if (ToolStripButton == btnBackward)
                    ActivePage--;
                else if (ToolStripButton == btnForward)
                    ActivePage++;
                else if (ToolStripButton == btnLast)
                    ActivePage = PagesCounter;
                else if (ToolStripButton == btnFirst)
                    ActivePage = 1;
                else
                    ActivePage = Convert.ToInt32(ToolStripButton.Text, System.Globalization.CultureInfo.InvariantCulture);

                if (ActivePage < 1)
                    ActivePage = 1;
                else if (ActivePage > PagesCounter)
                    ActivePage = PagesCounter;

                //Rebind the Datagridview with the data.
                RebindGridForPageChange();

                //Change the pagiantions buttons according to page number
                RefreshPagination();
            }
            catch (Exception) { }
        }



    }
}

 

listData class code: -


using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DatagridviewPaging
{
    class listData
    {
        public listData(string stuName, string stuClass)
        {
            this.stuName = stuName;
            this.stuClass = stuClass;

        }

        private string stuName;

        public string StudName
        {
            get { return stuName; }
            set { stuName = value; }
        }

        private string stuClass;

        public string StuClass
        {
            get { return stuClass; }
            set { stuClass = value; }
        }


    }
}

 

Output: -

Implement Paging in Datagridview C#


Updated 25-Nov-2019

Leave Comment

Comments

Liked By