C#DataGridView使用BindingNavigator實(shí)現(xiàn)簡(jiǎn)單分頁(yè)功能

c#
小云
219
2023-09-15 05:36:46

要使用BindingNavigator實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)功能,可以按照以下步驟進(jìn)行操作:

  1. 在窗體上添加一個(gè)DataGridView控件和一個(gè)BindingNavigator控件。

  2. 在窗體的Load事件中,使用數(shù)據(jù)源填充DataGridView控件,并將BindingNavigator控件的BindingSource屬性設(shè)置為DataGridView控件的數(shù)據(jù)源。

  3. 設(shè)置DataGridView控件的屬性,包括AllowUserToAddRows、AllowUserToDeleteRows和SelectionMode等。

  4. 設(shè)置BindingNavigator控件的屬性,包括AddNewItem、DeleteItem和CountItem等。

  5. 在BindingNavigator控件的Events中,添加點(diǎn)擊“上一頁(yè)”和“下一頁(yè)”按鈕的事件處理程序。

  6. 在事件處理程序中,修改BindingSource控件的Position屬性,實(shí)現(xiàn)數(shù)據(jù)的翻頁(yè)。

以下是一個(gè)簡(jiǎn)單的示例代碼:

public partial class Form1 : Form
{
private BindingSource bindingSource = new BindingSource();
private int pageSize = 10; // 每頁(yè)顯示的記錄數(shù)
private int currentPage = 1; // 當(dāng)前頁(yè)碼
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 使用數(shù)據(jù)源填充DataGridView控件
// 可以使用自己的數(shù)據(jù)源替換下面的示例數(shù)據(jù)
List<Person> persons = GetPersons();
bindingSource.DataSource = persons;
dataGridView1.DataSource = bindingSource;
// 設(shè)置DataGridView控件的屬性
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
// 設(shè)置BindingNavigator控件的屬性
bindingNavigator1.BindingSource = bindingSource;
bindingNavigator1.AddNewItem.Enabled = false;
bindingNavigator1.DeleteItem.Enabled = false;
// 設(shè)置分頁(yè)信息
int pageCount = (int)Math.Ceiling(persons.Count / (double)pageSize);
bindingNavigator1.CountItem.Text = "共 " + pageCount + " 頁(yè)";
bindingNavigator1.MoveFirstItem.Click += new EventHandler(MoveFirstItem_Click);
bindingNavigator1.MovePreviousItem.Click += new EventHandler(MovePreviousItem_Click);
bindingNavigator1.MoveNextItem.Click += new EventHandler(MoveNextItem_Click);
bindingNavigator1.MoveLastItem.Click += new EventHandler(MoveLastItem_Click);
}
private void MoveFirstItem_Click(object sender, EventArgs e)
{
currentPage = 1;
bindingSource.Position = 0;
}
private void MovePreviousItem_Click(object sender, EventArgs e)
{
if (currentPage > 1)
{
currentPage--;
bindingSource.Position -= pageSize;
}
}
private void MoveNextItem_Click(object sender, EventArgs e)
{
int pageCount = (int)Math.Ceiling(bindingSource.Count / (double)pageSize);
if (currentPage < pageCount)
{
currentPage++;
bindingSource.Position += pageSize;
}
}
private void MoveLastItem_Click(object sender, EventArgs e)
{
int pageCount = (int)Math.Ceiling(bindingSource.Count / (double)pageSize);
currentPage = pageCount;
bindingSource.Position = (currentPage - 1) * pageSize;
}
private List<Person> GetPersons()
{
// 示例數(shù)據(jù)
List<Person> persons = new List<Person>();
for (int i = 1; i <= 100; i++)
{
persons.Add(new Person { Id = i, Name = "Person " + i });
}
return persons;
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}

在這個(gè)示例中,我們使用一個(gè)包含100個(gè)Person對(duì)象的List作為數(shù)據(jù)源,每頁(yè)顯示10條記錄??梢愿鶕?jù)自己的需求修改pageSize和數(shù)據(jù)源。

0