C# BindingSource 未完BindingNavigator


C#中, BindingSource 是一个非常有用的控件,它提供了数据绑定的基础设施。 BindingSource 允许开发者将数据源(如数据库、集合、对象等)与用户界面控件(如文本框、下拉列表等)进行绑定,从而实现数据的展示和编辑。

以下是一些关于 BindingSource 的关键点:

数据绑定

BindingSource 可以绑定到多种数据源。以下是一些你可以绑定到 BindingSource 的数据源类型:

  • 列表(List):任何实现了 IList 接口的集合,例如 List<T>、ArrayList 等。
  • 数组(Array):任何类型的数组,如 int[]、string[]、object[] 等。
  • 数据表(DataTable):DataTable 是 DataSet 的一部分,常用于 ADO.NET 应用程序中。
  • 数据集(DataSet):一个包含多个 DataTable 的容器,可以看作是内存中的数据库。
  • 数据视图(DataView):一个 DataTable 的自定义视图,可以用来排序和筛选数据。
  • 实体框架(Entity Framework):使用 Entity Framework 时,可以将 DbSet<T> 或查询结果绑定到 BindingSource。
  • LINQ 查询结果:任何返回序列的 LINQ 查询,如 IEnumerable<T>、IQueryable<T> 等。
  • 自定义集合:任何自定义的集合类,只要它们实现了 IList 或 IBindingList 接口。
  • 单值:虽然不常见,但 BindingSource 也可以绑定到单个对象。

导航

C#Windows窗体应用程序中,BindingSource 组件提供了内置的数据导航功能,允许你轻松地在绑定的数据集中移动。以下是一些常用的导航方法:

  • MoveFirst():将当前记录位置移动到数据集中的第一条记录。
  • MoveLast():将当前记录位置移动到数据集中的最后一条记录。
  • MoveNext():将当前记录位置向前移动一条记录。如果当前已经是最后一条记录,则不会有任何移动。
  • MovePrevious():将当前记录位置向后移动一条记录。如果当前已经是第一条记录,则不会有任何移动。
  • Position 属性:获取或设置当前记录的位置。这个属性是基于0的,所以第一条记录的位置是0

以下是一个简单的例子,展示了如何在Windows窗体应用程序中使用 BindingSource 进行数据导航:

public partial class Form1 : Form
{
    private BindingSource bindingSource1 = new BindingSource();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // 假设有一个 DataTable 叫做 myDataTable
        bindingSource1.DataSource = myDataTable;
        dataGridView1.DataSource = bindingSource1;
    }

    private void buttonFirst_Click(object sender, EventArgs e)
    {
        bindingSource1.MoveFirst();
    }

    private void buttonPrevious_Click(object sender, EventArgs e)
    {
        bindingSource1.MovePrevious();
    }

    private void buttonNext_Click(object sender, EventArgs e)
    {
        bindingSource1.MoveNext();
    }

    private void buttonLast_Click(object sender, EventArgs e)
    {
        bindingSource1.MoveLast();
    }
}

在这个例子中,我们创建了一个 BindingSource 对象,并将其绑定到一个 DataGridView 控件。我们还添加了一些按钮,用于导航数据。每个按钮的点击事件都会调用 BindingSource 的相应导航方法,从而在数据集中移动当前记录的位置。

此外,BindingSource 还会触发一些事件,如 PositionChangedCurrentChanged,这些事件在导航操作发生时会触发,允许你执行额外的逻辑,比如更新用户界面以反映当前记录的变化。

事件

BindingSource 支持多种事件,如 CurrentChangedPositionChangedDataSourceChanged 等,这些事件可以在数据或导航状态改变时触发。

数据验证

BindingSource 可以与 BindingNavigator 控件一起使用,提供数据验证和错误处理机制。

自定义

BindingSource 允许开发者自定义数据绑定逻辑,包括数据格式转换、数据过滤等。

示例

下面是一个简单的示例,展示如何在 WinForms 应用程序中使用 BindingSource

// 创建 BindingSource 实例
BindingSource bindingSource = new BindingSource();

// 设置数据源,这里假设有一个名为 customers 的 List<Customer> 类型的数据集合
bindingSource.DataSource = customers;

// 将 BindingSource 绑定到一个 DataGridView 控件
dataGridView1.DataSource = bindingSource;

// 导航到第一条记录
bindingSource.MoveFirst();

// 导航到下一条记录
bindingSource.MoveNext();

在这个示例中,customers 是一个包含 Customer 对象的列表,每个 Customer 对象代表一个客户。通过 BindingSource,我们可以将这个列表绑定到 DataGridView 控件上,从而在界面上展示客户数据。

示例

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 forForm2019 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            allStudents.Add(new Student { age = 1, name = "a" });
            allStudents.Add(new Student { age = 1, name = "a" });

            bindingSource1.DataSource = allStudents;
            dataGridView1.DataSource = bindingSource1;

            num.count = allStudents.Count;
            bs_lable.DataSource = num;
            label1.DataBindings.Add("Text", bs_lable, "count");
        }
        List<Student> allStudents = new List<Student>();
        BindingSource bs_lable = new BindingSource();
        Number num = new Number();

        private void button1_Click(object sender, EventArgs e) {
            allStudents.Add(new Student { age = 1, name = "a" });
            num.count = allStudents.Count;

            bindingSource1.ResetBindings(false);
            bs_lable.ResetBindings(false);
        }

    }

    class Student {
        public int age { get; set; }

        public string name { get; set; }
    }

    class Number {
        public int count { get; set; }
        public int index { get; set; }
    }
}

在这里插入图片描述

上述代码有两个绑定,一是ListDataView,二是CountLable,不能绑定单个int,但能绑定类。

总结

BindingSource 是数据绑定中一个非常强大的工具,它简化了数据与UI控件之间的交互,使得数据操作更加直观和方便。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值