Question

I need to create paging in datagridview and I was found a demo in programCall.com successfully and that’s link ProgramCall.com

Below is screen shot of downloaded demo Form:- enter image description here

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

Below screen shot is update. enter image description here

Here is my question; when I have checked a checkbox in first page and after that move to Second page to perform any task after returning in first page the my checked checkboxes is unchecked, I want to maintain its checked status so any property 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.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!

Was it helpful?

Solution

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 Property to link the column to a model property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top