.NET Framework 类库BindingNavigator 类

BindingNavigator 控件表示在窗体上定位和操作数据的标准化方法。多数情况下,BindingNavigator

默认情况下,BindingNavigator 控件的用户界面 (UI) 由一系列 ToolStrip 按钮、文本框和静态文本元素组成,用于进行大多数常见的数据相关操作(如添加数据、删除数据和在数据中导航)。每个控件都可以通过 BindingNavigator 控件的关联成员进行检索或设置。类似地,还与以编程方式执行相同功能的 BindingSource 类的成员存在一一对应关系,如下表所示。

UI 控件

BindingNavigator 成员

BindingSource 成员

移到最前

MoveFirstItem

MoveFirst

前移一步

MovePreviousItem

MovePrevious

当前位置

PositionItem

Current

计数

CountItem

Count

移到下一条记录

MoveNextItem

MoveNext

移到最后

MoveLastItem

MoveLast

新添

AddNewItem

AddNew

删除

DeleteItem

RemoveCurrent

BindingNavigator 控件添加到窗体并绑定到数据源(例如 BindingSource)时,将自动在此表中建立关系。

BindingNavigator 的所有构造函数都调用 AddStandardItems 方法以将标准的 UI 控件集与导航工具栏关联起来。可使用以下技术之一自定义此工具栏:

  • 创建带有 BindingNavigator(Boolean) 构造函数的 BindingNavigator,此构造函数接受 Boolean 型的 addStandardItems 参数,并将此参数设置为 false。然后将需要的 ToolStripItem 对象添加到 Items 集合。

  • 如果需要进行大量的自定义设置,或者将重复使用自定义设计,应从 BindingNavigator 派生一个类并重写 AddStandardItems 方法以定义附加标准项或替换标准项。

BindingSource 控件成对出现,用于浏览窗体上的数据记录,并与它们交互。在这些情况下, BindingSource 属性被设置为作为数据源的关联 System.Windows.Forms.BindingSource 组件。
  示例

下面的代码示例演示如何使用 BindingNavigator 控件浏览数据库查询结果。结果集包含在

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;

// This form demonstrates using a BindingNavigator to display 
// rows from a database query sequentially.
public class Form1 : Form
{
    // This is the BindingNavigator that allows the user
    // to navigate through the rows in a DataSet.
    BindingNavigator customersBindingNavigator = new BindingNavigator();

    // This is the BindingSource that provides data for
    // the Textbox control.
    BindingSource customersBindingSource = new BindingSource();

    // This is the TextBox control that displays the CompanyName
    // field from the the DataSet.
    TextBox companyNameTextBox = new TextBox();

    public Form1()
    {
        // Set up the BindingSource component.
        this.customersBindingNavigator.BindingSource = this.customersBindingSource;
        this.customersBindingNavigator.Dock = DockStyle.Top;
        this.Controls.Add(this.customersBindingNavigator);

        // Set up the TextBox control for displaying company names.
        this.companyNameTextBox.Dock = DockStyle.Bottom;
        this.Controls.Add(this.companyNameTextBox);

        // Set up the form.
        this.Size = new Size(800, 200);
        this.Load += new EventHandler(Form1_Load);
    }

    void Form1_Load(object sender, EventArgs e)
    {   
        // Open a connection to the database.
        // Replace the value of connectString with a valid 
        // connection string to a Northwind database accessible 
        // to your system.
        string connectString = 
            "Integrated Security=SSPI;Persist Security Info=False;" +
            "Initial Catalog=Northwind;Data Source=localhost";
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = connectString;
        connection.Open();

        // Execute the query.
        SqlCommand command = new SqlCommand(
            "Select * From Customers", connection);
        SqlDataReader reader = command.ExecuteReader(
            CommandBehavior.CloseConnection);

        // Load the Customers result set into the DataSet.
        DataSet ds = new DataSet("Northwind Customers");
        ds.Load(
            reader, 
            LoadOption.OverwriteChanges, 
            new string[] { "Customers" });

        // Assign the DataSet as the DataSource for the BindingSource.
        this.customersBindingSource.DataSource = ds;

        // Bind the CompanyName field to the TextBox control.
        this.companyNameTextBox.DataBindings.Add(
            new Binding("Text", 
            this.customersBindingSource, 
            "CompanyName", 
            true));
    }
}
DataSet 中,用 BindingSource 组件将它绑定到 TextBox 控件。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先,在项目中添加一个DataGridView控件,并设置其数据源,例如: ``` dataGridView1.DataSource = myDataTable; ``` 2. 接着,在窗体上添加一个分页控件,例如: ``` private void Form1_Load(object sender, EventArgs e) { BindingSource bs = new BindingSource(); bs.DataSource = myDataTable; dataGridView1.DataSource = bs; BindingNavigator bn = new BindingNavigator(true); bn.BindingSource = bs; this.Controls.Add(bn); } ``` 3. 在分页控件中设置每页显示的记录数和总记录数,例如: ``` bn.CountItem.Text = "总记录数:" + myDataTable.Rows.Count.ToString() + "条"; bn.PositionItem.Visible = false; bn.Items.Add("每页显示:"); ToolStripComboBox tscb = new ToolStripComboBox(); tscb.Items.Add("10"); tscb.Items.Add("20"); tscb.Items.Add("50"); tscb.SelectedIndex = 0; tscb.SelectedIndexChanged += new EventHandler(tscb_SelectedIndexChanged); bn.Items.Add(tscb); ``` 4. 在分页控件中添加翻页按钮,并设置其事件处理程序,例如: ``` bn.Items.Add("第"); ToolStripTextBox tstb = new ToolStripTextBox(); tstb.Text = "1"; tstb.Width = 50; bn.Items.Add(tstb); bn.Items.Add("页"); bn.Items.Add("共" + (myDataTable.Rows.Count / 10 + 1).ToString() + "页"); bn.Items.Add(new ToolStripButton("首页")); bn.Items.Add(new ToolStripButton("上一页")); bn.Items.Add(new ToolStripButton("下一页")); bn.Items.Add(new ToolStripButton("末页")); bn.Items.Add(new ToolStripLabel("跳转到")); ToolStripTextBox tstb2 = new ToolStripTextBox(); tstb2.Text = "1"; tstb2.Width = 50; bn.Items.Add(tstb2); bn.Items.Add(new ToolStripButton("确定")); bn.Items.Add(new ToolStripLabel("页")); bn.ItemClicked += new ToolStripItemClickedEventHandler(bn_ItemClicked); ``` 5. 在事件处理程序中,根据用户的操作计算出当前页码和每页显示的记录数,并重新绑定数据源,例如: ``` private void tscb_SelectedIndexChanged(object sender, EventArgs e) { ToolStripComboBox tscb = (ToolStripComboBox)sender; int pageSize = int.Parse(tscb.SelectedItem.ToString()); BindingSource bs = (BindingSource)dataGridView1.DataSource; bs.DataSource = myDataTable; bs.Filter = ""; bs.Sort = ""; dataGridView1.DataSource = bs; BindingNavigator bn = (BindingNavigator)dataGridView1.BindingNavigator; bn.CountItem.Text = "总记录数:" + myDataTable.Rows.Count.ToString() + "条"; bn.PositionItem.Visible = false; bn.Items[7].Text = "共" + (myDataTable.Rows.Count / pageSize + 1).ToString() + "页"; bn.Items[3].Text = "第1页"; if (myDataTable.Rows.Count > pageSize) { bs.DataSource = myDataTable.AsEnumerable().Take(pageSize).CopyToDataTable(); } } private void bn_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { BindingSource bs = (BindingSource)dataGridView1.DataSource; int pageSize = int.Parse(((ToolStripComboBox)bs.Position).SelectedItem.ToString()); int pageCount = myDataTable.Rows.Count / pageSize + 1; int currentPage = int.Parse(bs.PositionItem.Text.Substring(2, bs.PositionItem.Text.Length - 3)); switch (e.ClickedItem.Text) { case "首页": currentPage = 1; break; case "上一页": currentPage--; break; case "下一页": currentPage++; break; case "末页": currentPage = pageCount; break; case "确定": currentPage = int.Parse(bs.PositionItem.Text.Substring(2, bs.PositionItem.Text.Length - 3)); int gotoPage = int.Parse(((ToolStripTextBox)bs.PositionItem).Text); if (gotoPage >= 1 && gotoPage <= pageCount) { currentPage = gotoPage; } break; } bs.DataSource = myDataTable.AsEnumerable().Skip((currentPage - 1) * pageSize).Take(pageSize).CopyToDataTable(); bs.PositionItem.Text = "第" + currentPage.ToString() + "页"; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值