forum

Home / DeveloperSection / Forums / how to maintain checkbox check status on paging link click in datagridview in C#

how to maintain checkbox check status on paging link click in datagridview in C#

Anonymous User584005-Jun-2013

Hi Expert,

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:- 

how to maintain checkbox check status on paging link click in datagridview in C#

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#

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{publicpartialclassForm1:Form{privateintCurrentPage=1;intPagesCount=1;int pageRows =10;BindingList<Employee>Baselist=null;BindingList<Employee>Templist=null;publicForm1(){InitializeComponent();DataGridViewCheckBoxColumnCheckboxColumn=newDataGridViewCheckBoxColumn();CheckboxColumn.TrueValue=true; dataGridView1.Columns.Add(CheckboxColumn); dataGridView1.Rows.Add(1); dataGridView1.Columns[0].HeaderText="Select"; dataGridView1.Columns[0].Width=50;}privatevoidForm1_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 dataprivateBindingList<Employee>FillDataforGrid(){BindingList<Employee>list=newBindingList<Employee>();for(int i =1; i <=20; i++){Employee obj =newEmployee(i,"ProgramCall.com"+ i.ToString(),"Finance"+ i.ToString());list.Add(obj);}returnlist;}privatevoidRebindGridForPageChange(){//Rebinding the Datagridview with dataint datasourcestartIndex =(CurrentPage-1)* pageRows;Templist=newBindingList<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 clicksprivatevoidToolStripButtonClick(object sender,EventArgs e){try{ToolStripButtonToolStripButton=((ToolStripButton)sender);//Determining the current pageif(ToolStripButton== btnBackward)CurrentPage--;elseif(ToolStripButton== btnForward)CurrentPage++;elseif(ToolStripButton== btnLast)CurrentPage=PagesCount;elseif(ToolStripButton== btnFirst)CurrentPage=1;elseCurrentPage=Convert.ToInt32(ToolStripButton.Text,CultureInfo.InvariantCulture);if(CurrentPage<1)CurrentPage=1;elseif(CurrentPage>PagesCount)CurrentPage=PagesCount;//Rebind the Datagridview with the data.RebindGridForPageChange();//Change the pagiantions buttons according to page numberRefreshPagination();}catch(Exception){}}privatevoidRefreshPagination(){ToolStripButton[] items =newToolStripButton[]{ 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 buttonsif(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 buttonsif(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{classEmployee{publicEmployee(int empid,string empname,string empdept){this.empId = empid;this.empName = empname;this.empDept = empdept;}privateint empId;publicintEmpid{ get {return empId;}set{ empId = value;}}privatestring empName;publicstringEmpName{ get {return empName;}set{ empName = value;}}privatestring empDept;publicstringEmpDept{ get {return empDept;}set{ empDept = value;}}}}

Thank You in Advanced!




Updated on 08-Jul-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By