我需要在datagridview中创建分页,我在programCall.com中成功找到了一个演示,并且链接ProgramCall.com
以下是下载的演示表格的屏幕截图: -
现在我更新下载的代码并在datagridview的第0(0)索引中添加一个复选框列: -
屏幕截图下方是更新。
这是我的问题;当我在第一页中选中了一个复选框后,在第一页返回之后执行任何任务后,在第一页中返回后,我的复选框被取消选中,我想保持其已检查状态,以便任何属性或方法都可以维护datagridview复选框检查状态或者。
下面我给你更新的代码: -
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; }
}
}
}
先谢谢你!
答案 0 :(得分:1)
它的发生是因为复选框列是动态的,并且没有链接到模型中的属性。因此,更改不会持续存在于页面中。
员工类必须有一个属性,比如bool Checked
。我认为datagrid会自动为此创建一个复选框列。如果没有,则复选框列应绑定到此属性。
使用DataGridViewColumn.DataPropertyName属性将列链接到模型属性。