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# winform组件 怎样集成

    在C# WinForms应用程序中集成组件通常涉及以下几个步骤: 添加引用: 首先,确保你已经安装了所需的组件。例如,如果你想要集成一个第三方库,你需要将该库的DLL...

  • c# winform组件 怎样提高效率

    要提高C# WinForms应用程序的效率,可以从多个方面入手,包括优化绘图过程、使用异步操作、合理设置控件属性等。以下是一些具体的优化策略:
    优化绘图过程 ...

  • c# winform组件 怎样自定义

    在C#中,要自定义WinForms组件,您需要创建一个继承自现有控件的新类,并重写其构造函数、事件处理程序和其他相关方法。以下是一个简单的示例,说明如何创建一个...

  • c# winform组件 怎样选择合适

    在C# WinForms应用程序开发中,选择合适的组件对于构建高效、用户友好的界面至关重要。以下是一些选择合适组件的建议:
    控件选择 根据需求选择:选择控件时...

  • 在处理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()...