117.info
人生若只如初见

C#DataGridView使用BindingNavigator实现简单分页功能

要使用BindingNavigator实现简单的分页功能,可以按照以下步骤进行操作:

  1. 在窗体上添加一个DataGridView控件和一个BindingNavigator控件。

  2. 在窗体的Load事件中,使用数据源填充DataGridView控件,并将BindingNavigator控件的BindingSource属性设置为DataGridView控件的数据源。

  3. 设置DataGridView控件的属性,包括AllowUserToAddRows、AllowUserToDeleteRows和SelectionMode等。

  4. 设置BindingNavigator控件的属性,包括AddNewItem、DeleteItem和CountItem等。

  5. 在BindingNavigator控件的Events中,添加点击“上一页”和“下一页”按钮的事件处理程序。

  6. 在事件处理程序中,修改BindingSource控件的Position属性,实现数据的翻页。

以下是一个简单的示例代码:

public partial class Form1 : Form
{
private BindingSource bindingSource = new BindingSource();
private int pageSize = 10; // 每页显示的记录数
private int currentPage = 1; // 当前页码
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 使用数据源填充DataGridView控件
// 可以使用自己的数据源替换下面的示例数据
List persons = GetPersons();
bindingSource.DataSource = persons;
dataGridView1.DataSource = bindingSource;
// 设置DataGridView控件的属性
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
// 设置BindingNavigator控件的属性
bindingNavigator1.BindingSource = bindingSource;
bindingNavigator1.AddNewItem.Enabled = false;
bindingNavigator1.DeleteItem.Enabled = false;
// 设置分页信息
int pageCount = (int)Math.Ceiling(persons.Count / (double)pageSize);
bindingNavigator1.CountItem.Text = "共 " + pageCount + " 页";
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 GetPersons()
{
// 示例数据
List persons = new List();
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; }
}

在这个示例中,我们使用一个包含100个Person对象的List作为数据源,每页显示10条记录。可以根据自己的需求修改pageSize和数据源。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fef55AzsLBwVeBlE.html

推荐文章

  • C#中Environment.Exit(0)和Environment.Exit()方法有什么不同

    在C#中,Environment.Exit(0)和Environment.Exit()方法都用于终止应用程序的执行,但它们之间有一些不同之处。 参数:Environment.Exit(0)方法接受一个整数参数,...

  • c++捕获异常的方法是什么

    在C++中,可以使用try-catch块来捕获异常。试图在try块中执行代码,如果发生异常,则会立即跳转到与异常类型匹配的catch块。catch块可以处理异常,并执行相应的操...

  • C#中DirectoryEntry对象怎么使用

    DirectoryEntry对象在C#中用于操作和管理目录服务(也称为Active Directory)中的目录项。下面是一些常用的DirectoryEntry对象的使用方法: 创建一个DirectoryEn...

  • C++ internetopen函数的用法是什么

    InternetOpen函数是Windows平台上的一个API函数,用于打开一个指向Internet的句柄。
    它的函数原型如下:
    HINTERNET InternetOpen(
    _In_ LPCTSTR ...

  • 在处理PowerBuilder的itemchanged事件中,acceptText的使用

    在PowerBuilder的itemchanged事件处理中,acceptText是一个函数,用于接受用户在编辑控件中输入的文本。它的语法如下:
    acceptText ( [control] )
    其中...

  • .NET中的repeater简介及分页效果

    Repeater是.NET中的一个控件,它允许开发人员通过绑定数据源来重复显示相同的模板内容。它在网页上生成重复的HTML代码,可以根据数据库或其他数据源中的数据来动...

  • Dataformatstring属性设置(详解)

    DataFormatString属性是用于设置数据呈现格式的属性。它可用于各种数据控件,如Label、GridView、DataList等。
    可以使用DataFormatString属性来指定数据的格...

  • C#中timer的用法

    在C#中,我们可以使用Timer类来实现定时器的功能。Timer类位于System.Timers命名空间中。以下是Timer类的基本用法: 创建Timer对象: Timer timer = new Timer()...