---
title: "how to maintain checkbox check status on paging link click in datagridview in C#"  
description: "how to maintain checkbox check status on paging link click in datagridview in C#"  
author: "Anonymous User"  
published: 2013-06-05  
updated: 2013-07-08  
canonical: https://www.mindstick.com/forum/994/how-to-maintain-checkbox-check-status-on-paging-link-click-in-datagridview-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# how to maintain checkbox check status on paging link click in datagridview in C#

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances),

I need to create paging in [datagridview](https://www.mindstick.com/articles/607/working-with-datagridview-in-vc-sharp) and I was found a demo in programCall.com successfully and that’s link ProgramCall.com

Below is [screen](https://www.mindstick.com/forum/33644/loading-screen-during-ajax-call) shot of downloaded demo Form:-

![how to maintain checkbox check status on paging link click in datagridview in C#](http://i.stack.imgur.com/cRPlJ.png)

Now I update this downloaded code and add a check box column in zeroth (0) index of datagridview:-

Below screen shot is update.

![how to maintain checkbox check status on paging link click in datagridview in C#](http://i.stack.imgur.com/0sT6s.png)

Here is my [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time); when I have checked a [checkbox](https://www.mindstick.com/articles/291/how-should-use-checkbox-control-in-vb-dot-net) in first page and after that move to [Second page](https://www.mindstick.com/forum/23130/windows-phone-8-1-universal-app-terminates-on-navigating-back-from-second-page) to perform any task after [returning](https://www.mindstick.com/news/2477/returning-ceo-bob-iger-dismisses-the-disney-apple-sale-as-pure-speculation) in first page the my checked checkboxes is unchecked, I want to maintain its checked status so any [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) or method is there to maintain datagridview checkbox checked status or else.

Below I am giving you my updated code:-

Form1.cs:-

```
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 System.Globalization;

namespace DatagridviewPagination
{
    public partial class Form1 : Form
    {

        private int CurrentPage = 1;
        int PagesCount = 1;
        int pageRows = 10;

        BindingList<Employee> Baselist = null;
        BindingList<Employee> Templist = null;

        public Form1()
        {
            InitializeComponent();
            DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckboxColumn.TrueValue = true;
            dataGridView1.Columns.Add(CheckboxColumn);
            dataGridView1.Rows.Add(1);
            dataGridView1.Columns[0].HeaderText = "Select";
            dataGridView1.Columns[0].Width = 50;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Baselist = FillDataforGrid();
            PagesCount = Convert.ToInt32(Math.Ceiling(Baselist.Count * 1.0 / pageRows));

            CurrentPage = 1;
            RefreshPagination();
            RebindGridForPageChange();
        }

        //Method to generate temporary data
        private BindingList<Employee> FillDataforGrid()
        {
            BindingList<Employee> list = new BindingList<Employee>();
            for (int i = 1; i <= 20; i++)
            {
                Employee obj = new Employee(i, "ProgramCall.com" + i.ToString(), "Finance" + i.ToString());
                list.Add(obj);
            }

            return list;
        }

        private void RebindGridForPageChange()
        {
            //Rebinding the Datagridview with data
            int datasourcestartIndex = (CurrentPage - 1) * pageRows;
            Templist = new BindingList<Employee>();
            for (int i = datasourcestartIndex; i < datasourcestartIndex + pageRows; i++)
            {
                if (i >= Baselist.Count)
                    break;

                Templist.Add(Baselist[i]);
            }

            dataGridView1.DataSource = Templist;
            dataGridView1.Refresh();
        }

        //Method that handles the pagination button clicks
        private void ToolStripButtonClick(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton ToolStripButton = ((ToolStripButton)sender);

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

                if (CurrentPage < 1)
                    CurrentPage = 1;
                else if (CurrentPage > PagesCount)
                    CurrentPage = PagesCount;

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

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

        private void RefreshPagination()
        {
            ToolStripButton[] items = new ToolStripButton[] { toolStripButton1, toolStripButton2, toolStripButton3, toolStripButton4, toolStripButton5 };

            //pageStartIndex contains the first button number of pagination.
            int pageStartIndex = 1;

            if (PagesCount > 5 && CurrentPage > 2)
                pageStartIndex = CurrentPage - 2;

            if (PagesCount > 5 && CurrentPage > PagesCount - 2)
                pageStartIndex = PagesCount - 4;

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

                    //Setting the Appearance of the page number buttons
                    if (i == CurrentPage)
                    {
                        items[i - pageStartIndex].BackColor = Color.Black;
                        items[i - pageStartIndex].ForeColor = Color.White;
                    }
                    else
                    {
                        items[i - pageStartIndex].BackColor = Color.White;
                        items[i - pageStartIndex].ForeColor = Color.Black;
                    }
                }
            }

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

            if (CurrentPage == PagesCount)
                btnForward.Enabled = btnLast.Enabled = false;

            else
                btnForward.Enabled = btnLast.Enabled = true;
        }
    }
}
```

\

## [Employee](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks).cs:-

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DatagridviewPagination
{
    class Employee
    {

        public Employee(int empid, string empname, string empdept)
        {
            this.empId = empid;
            this.empName = empname;
            this.empDept = empdept;

        }
        private int empId;

        public int Empid
        {
            get { return empId; }
            set { empId = value; }
        }

        private string empName;

        public string EmpName
        {
            get { return empName; }
            set { empName = value; }
        }

        private string empDept;

        public string EmpDept
        {
            get { return empDept; }
            set { empDept = value; }
        }

    }
}
```

Thank You in [Advanced](https://www.mindstick.com/articles/12993/5-advanced-features-for-your-odoo-ecommerce-theme)!

\

\

## Replies

### Reply by Vijay Shukla

Hey Pravesh Singh!Its happening because the checkbox column is dynamic and not linked to a property in the Model. So the change is not persisted across pages.\
The employee class must have a property, say bool Checked. I think the datagrid will automatically create a checkbox column for this. If not, the checkbox column should be bound to this property.\
Use [DataGridViewColumn.DataPropertyName](http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.datapropertyname.aspx) Property to link the column to a model property.


---

Original Source: https://www.mindstick.com/forum/994/how-to-maintain-checkbox-check-status-on-paging-link-click-in-datagridview-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
