C# 如何在C中的datagridview中维护分页链接单击上的复选框检查状态#
我需要在datagridview中创建分页,我在programCall.com中成功地找到了一个演示,这就是链接 以下是下载的演示表单的屏幕截图:- 现在,我更新下载的代码,并在datagridview的第0(0)个索引中添加一个复选框列:- 下面的屏幕截图是更新。 这是我的问题;当我选中了第一页中的复选框,并在返回第一页后移动到第二页以执行任何任务时,我希望保持其选中状态,以便任何属性或方法都可以保持datagridview复选框选中状态或其他状态 下面是我的更新代码:- 表格1.cs:-C# 如何在C中的datagridview中维护分页链接单击上的复选框检查状态#,c#,datagridview,datagridviewcheckboxcell,C#,Datagridview,Datagridviewcheckboxcell,我需要在datagridview中创建分页,我在programCall.com中成功地找到了一个演示,这就是链接 以下是下载的演示表单的屏幕截图:- 现在,我更新下载的代码,并在datagridview的第0(0)个索引中添加一个复选框列:- 下面的屏幕截图是更新。 这是我的问题;当我选中了第一页中的复选框,并在返回第一页后移动到第二页以执行任何任务时,我希望保持其选中状态,以便任何属性或方法都可以保持datagridview复选框选中状态或其他状态 下面是我的更新代码:- 表格1.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类必须有一个属性,例如
bool Checked
。我认为datagrid会自动为此创建一个复选框列。否则,复选框列应绑定到此属性
使用属性将列链接到模型属性
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; }
}
}
}