【问题标题】:BindingNavigator with DataGridView for Paging function?BindingNavigator 与 DataGridView 的分页功能?
【发布时间】:2014-02-20 07:14:28
【问题描述】:

伙计们,我正在尝试通过 C# windows 窗体应用程序中的 bindingnavigator 在我的 datagridview 中实现分页。

我只是将 datagridview 和 bindingnavigator 从工具栏拖到窗体中。 Datagridview 使用数据集将数据绑定到 SQL 服务器中的数据库表。我在 gridview 中添加了 3 个额外的按钮,它们将执行一些功能。

现在我之前从未使用过 bindingnavigator,所以我只是从它的属性中选择了 datagridview1 的 bindingsource 作为 bindingnavigator 的数据源。

这是我的表单在运行时的样子:

目前,datagridview1 显示我的表中的所有记录(截至目前为 31 条),绑定导航器下一个按钮只会将我带到下一条记录(例如,从 TicketID=1 到 TicketID=2)。

现在,我想做的是:

1.) Datagridview 应该只显示每页 10(或 50)条记录,并且应该使用 bindingnavigator 控件在页面之间切换,以便 UI 保持响应并且变得更加“内存效率”,因为最终我的数据库将拥有数千条记录。

2.) BindingNavigator 的控件应该出现在表单的中心,而不是左/右。我无法从属性将其设置为居中。

我的表单后面的代码:

       using System;
       using System.Collections.Generic;
       using System.ComponentModel;
       using System.Data;
       using System.Drawing;
       using System.Linq;
       using System.Text;
       using System.Threading.Tasks;
       using System.Windows.Forms;

       namespace WindowsFormsApplication2
       {
           public partial class Form9 : Form
           {
               public Form9()
    {
        InitializeComponent();
    }

    private void Form9_Load(object sender, EventArgs e)
    {
        this.CenterToScreen();
        try
        {
            this.tblTicketDetailTableAdapter.Fill(this.sTDataSet4.tblTicketDetail);
        }
        catch
        {
            MessageBox.Show("Error : Cannot establish a valid connection to database.", "SQL SERVER ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dataGridView1.Columns["Detail"].Index)
        {
            //some code
        }
        else if (e.ColumnIndex == dataGridView1.Columns["Close"].Index)
        {
            //some code
        }
        else if (e.ColumnIndex == dataGridView1.Columns["ViewDetail"].Index)
        {
            //some code
        }
    }

}

现在我该怎么做才能让bindingnavigator 充当分页控件?

【问题讨论】:

  • 查看this SO问题的答案

标签: c# winforms datagridview bindingnavigator


【解决方案1】:

执行此操作的一种方法是将父表分解为 DataTables 列表(或我的偏好,BindingList<DataTable>),然后在导航器控件的位置更改上分配网格的 DataSource:

BindingSource bs = new BindingSource();
BindingList<DataTable> tables = new BindingList<DataTable>();

protected override void OnLoad(EventArgs e) {
  base.OnLoad(e);

  int counter = 0;
  DataTable dt = null;
  foreach (DataRow dr in tblTicketDetail.Rows) {
    if (counter == 0) {
      dt = tblTicketDetail.Clone();
      tables.Add(dt);
    }
    dt.Rows.Add(dr.ItemArray);
    ++counter;
    if (counter > 9) {
      counter = 0;
    }
  }
  bindingNavigator1.BindingSource = bs;
  bs.DataSource = tables;
  bs.PositionChanged += bs_PositionChanged;
  bs_PositionChanged(bs, EventArgs.Empty);
}

void bs_PositionChanged(object sender, EventArgs e) {
  dataGridView1.DataSource = tables[bs.Position];
}

就 BindingNavigator 控件居中而言,只需将 Dock 样式设置为 None 并手动将控件定位在中心。设置后,将 Anchors 设置为 none,并且在调整容器大小时 ToolBar 应该“浮动”在中心。

【讨论】:

  • 哇,好简单!对于 dgv 中的分页,微软有虚拟模式,当我读到它时,这似乎相当复杂。
  • @LarsTech 假设您的 DataGridView 中有 2 页数据,并且您将第二页上的一个项目的日期更改为早于第一页或第二页上的任何项目的日期,然后对DataGridView 按日期列。我希望该项目首先出现在第一页上,但它只显示为第二页上的第一个项目,即第二个 DataTable。我不确定如何执行此操作,但我认为经过排序后,我需要以某种方式重新计算每个 DataTable 中的项目并刷新 DataGridView 以显示 DataTables 的更新内容。
  • @Rich 没有内置任何内容,因此您要么自己手动将行项移动到正确的位置,要么重新对所有页面重新分页。
猜你喜欢
  • 2012-12-11
  • 2010-12-28
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
相关资源
最近更新 更多