BindingSource Class


封装窗体的数据源。Encapsulates the data source for a form.

public ref class BindingSource : System::ComponentModel::Component, System::Collections::IList, System::ComponentModel::IBindingListView, System::ComponentModel::ICancelAddNew, System::ComponentModel::ISupportInitialize, System::ComponentModel::ISupportInitializeNotification, System::ComponentModel::ITypedList, System::Windows::Forms::ICurrencyManagerProvider
public ref class BindingSource : System::ComponentModel::Component, System::Collections::IList, System::ComponentModel::IBindingListView, System::ComponentModel::ICancelAddNew, System::ComponentModel::ISupportInitializeNotification, System::ComponentModel::ITypedList, System::Windows::Forms::ICurrencyManagerProvider
[System.ComponentModel.ComplexBindingProperties("DataSource", "DataMember")]
public class BindingSource : System.ComponentModel.Component, System.Collections.IList, System.ComponentModel.IBindingListView, System.ComponentModel.ICancelAddNew, System.ComponentModel.ISupportInitialize, System.ComponentModel.ISupportInitializeNotification, System.ComponentModel.ITypedList, System.Windows.Forms.ICurrencyManagerProvider
[System.ComponentModel.ComplexBindingProperties("DataSource", "DataMember")]
public class BindingSource : System.ComponentModel.Component, System.Collections.IList, System.ComponentModel.IBindingListView, System.ComponentModel.ICancelAddNew, System.ComponentModel.ISupportInitializeNotification, System.ComponentModel.ITypedList, System.Windows.Forms.ICurrencyManagerProvider
type BindingSource = class
    inherit Component
    interface IBindingListView
    interface ICollection
    interface IEnumerable
    interface IList
    interface IBindingList
    interface ITypedList
    interface ICancelAddNew
    interface ISupportInitializeNotification
    interface ISupportInitialize
    interface ICurrencyManagerProvider
type BindingSource = class
    inherit Component
    interface IBindingListView
    interface IBindingList
    interface IList
    interface ICollection
    interface IEnumerable
    interface ITypedList
    interface ICancelAddNew
    interface ISupportInitializeNotification
    interface ISupportInitialize
    interface ICurrencyManagerProvider
Public Class BindingSource
Inherits Component
Implements IBindingListView, ICancelAddNew, ICurrencyManagerProvider, IList, ISupportInitialize, ISupportInitializeNotification, ITypedList
Public Class BindingSource
Inherits Component
Implements IBindingListView, ICancelAddNew, ICurrencyManagerProvider, IList, ISupportInitializeNotification, ITypedList


下面的代码示例演示绑定到 BindingSourceListBoxThe following code example demonstrates a ListBox bound to a BindingSource. BindingSource 绑定到包含字体列表的 BindingList<T>The BindingSource is bound to a BindingList<T> that contains a list of fonts.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BindingSourceExamples
    public class Form1 : Form
        static void Main()
            Application.Run(new Form1());

        public Form1()
            this.Load += new EventHandler(Form1_Load);

        private TextBox textBox1;
        private Button button1;
        private ListBox listBox1;
        private BindingSource binding1;
        void Form1_Load(object sender, EventArgs e)
            listBox1 = new ListBox();
            textBox1 = new TextBox();
            binding1 = new BindingSource();
            button1 = new Button();
            listBox1.Location = new Point(140, 25);
            listBox1.Size = new Size(123, 160);
            textBox1.Location = new Point(23, 70);
            textBox1.Size = new Size(100, 20);
            textBox1.Text = "Wingdings";
            button1.Location = new Point(23, 25);
            button1.Size = new Size(75, 23);
            button1.Text = "Search";
            button1.Click += new EventHandler(this.button1_Click);
            this.ClientSize = new Size(292, 266);

            MyFontList fonts = new MyFontList();
            for (int i = 0; i < FontFamily.Families.Length; i++)
                if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Regular))
                    fonts.Add(new Font(FontFamily.Families[i], 11.0F, FontStyle.Regular));
            binding1.DataSource = fonts;
            listBox1.DataSource = binding1;
            listBox1.DisplayMember = "Name";

        private void button1_Click(object sender, EventArgs e)
            if (binding1.SupportsSearching != true)
                MessageBox.Show("Cannot search the list.");
                int foundIndex = binding1.Find("Name", textBox1.Text);
                if (foundIndex > -1)
                    listBox1.SelectedIndex = foundIndex;
                    MessageBox.Show("Font was not found.");
    public class MyFontList : BindingList<Font>

        protected override bool SupportsSearchingCore
            get { return true; }
        protected override int FindCore(PropertyDescriptor prop, object key)
            // Ignore the prop value and search by family name.
            for (int i = 0; i < Count; ++i)
                if (Items[i].FontFamily.Name.ToLower() == ((string)key).ToLower())
                    return i;
            return -1;
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
    Inherits Form

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New Form1())

    End Sub

    Public Sub New()

    End Sub

    Private textBox1 As TextBox
    Private WithEvents button1 As Button
    Private listBox1 As ListBox
    Private components As IContainer
    Private binding1 As BindingSource

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        listBox1 = New ListBox()
        textBox1 = New TextBox()
        binding1 = New BindingSource()
        button1 = New Button()
        listBox1.Location = New Point(140, 25)
        listBox1.Size = New Size(123, 160)
        textBox1.Location = New Point(23, 70)
        textBox1.Size = New Size(100, 20)
        textBox1.Text = "Wingdings"
        button1.Location = New Point(23, 25)
        button1.Size = New Size(75, 23)
        button1.Text = "Search"
        Me.ClientSize = New Size(292, 266)

        Dim fonts As New MyFontList()
        Dim i As Integer
        For i = 0 To FontFamily.Families.Length - 1
            If FontFamily.Families(i).IsStyleAvailable(FontStyle.Regular) Then
                fonts.Add(New Font(FontFamily.Families(i), 11.0F, FontStyle.Regular))
            End If
        Next i
        binding1.DataSource = fonts
        listBox1.DataSource = binding1
        listBox1.DisplayMember = "Name"

    End Sub
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles button1.Click

        If binding1.SupportsSearching <> True Then
            MessageBox.Show("Cannot search the list.")
            Dim foundIndex As Integer = binding1.Find("Name", textBox1.Text)
            If foundIndex > -1 Then
                listBox1.SelectedIndex = foundIndex
                MessageBox.Show("Font was not found.")
            End If
        End If

    End Sub
End Class

Public Class MyFontList
    Inherits BindingList(Of Font)

    Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean
            Return True
        End Get
    End Property
    Protected Overrides Function FindCore(ByVal prop As PropertyDescriptor, _
        ByVal key As Object) As Integer
        ' Ignore the prop value and search by family name.
        Dim i As Integer
        While i < Count
            If Items(i).FontFamily.Name.ToLower() = CStr(key).ToLower() Then
                Return i
            End If
            i += 1
        End While

        Return -1
    End Function
End Class


BindingSource 组件提供多种用途。The BindingSource component serves many purposes. 首先,通过在 Windows 窗体控件和数据源之间提供货币管理、更改通知和其他服务,简化了窗体上的控件与数据的绑定。First, it simplifies binding controls on a form to data by providing currency management, change notification, and other services between Windows Forms controls and data sources. 这是通过使用 DataSource 属性将 BindingSource 组件附加到数据源来实现的。This is accomplished by attaching the BindingSource component to your data source using the DataSource property. 对于复杂的绑定方案,可以选择将 DataMember 属性设置为数据源中的特定列或列表。For complex binding scenarios you can optionally set the DataMember property to a specific column or list in the data source. 然后,将控件绑定到 BindingSourceYou then bind controls to the BindingSource. 与数据的所有进一步交互都是通过调用 BindingSource 组件来实现的。All further interaction with the data is accomplished with calls to the BindingSource component. 有关 BindingSource 如何简化绑定过程的示例,请参阅如何:将 Windows 窗体控件绑定到 DBNull 数据库值如何:处理数据绑定发生的错误和异常For examples on how the BindingSource can simplify the binding process, see How to: Bind Windows Forms Controls to DBNull Database Values and How to: Handle Errors and Exceptions that Occur with Databinding. 数据源的导航和更新通过 MoveNextMoveLastRemove等方法来完成。Navigation and updating of the data source is accomplished through methods such as MoveNext, MoveLast, and Remove. 排序和筛选等操作通过 SortFilter 属性进行处理。Operations such as sorting and filtering are handled through the Sort and Filter properties. 有关将排序和筛选用于 BindingSource的详细信息,请参阅如何:使用 Windows 窗体 BindingSource 组件对 ADO.NET 数据进行排序和筛选For more information on using sorting and filtering with the BindingSource, see How to: Sort and Filter ADO.NET Data with the Windows Forms BindingSource Component.

此外,BindingSource 组件可以充当强类型的数据源。In addition, the BindingSource component can act as a strongly typed data source. 通常,基础数据源的类型是通过以下机制之一固定的:Typically the type of the underlying data source is fixed through one of the following mechanisms:

这两种机制都创建强类型列表。Both of these mechanisms create a strongly-typed list. 有关如何使用 BindingSource 绑定到类型的详细信息,请参阅如何:将 Windows 窗体控件绑定到类型For more information on how to use the BindingSource to bind to a type, see How to: Bind a Windows Forms Control to a Type. 你还可以使用 BindingSource 将控件绑定到工厂对象。You can also use the BindingSource to bind your controls to a factory object. 有关如何执行此操作的详细信息,请参阅如何:将 Windows 窗体控件绑定到工厂对象For more information on how to do this, see How to: Bind a Windows Forms Control to a Factory Object.


由于 BindingSource 处理简单和复杂的数据源,因此术语是有问题的。Because a BindingSource handles both simple and complex data sources, terminology is problematic. 在此类文档中,术语 "列表" 指的是托管数据源中的数据集合,"" 表示单个元素。Within this class documentation, the term list refers to a data collection within the hosted data source, and item denotes a single element. 讨论与复杂数据源关联的功能时,将使用等效术语When discussing functionality associated with complex data sources, the equivalent terms table and row are used.

BindingSource 提供访问基础数据的成员。BindingSource provides members for accessing the underlying data. 可以通过 Current 属性检索当前项,整个列表可以通过 List 属性进行检索。The current item can be retrieved through the Current property, and the entire list can be retrieved through the List property. CurrentRemoveCurrentEndEditCancelEdit 以及 AddAddNew 方法,当前项支持编辑操作。Editing operations are supported on the current item through Current and the RemoveCurrent, EndEdit, CancelEdit and Add and AddNew methods. 尽管自动为所有基础数据源类型处理币种管理,但此类公开了许多事件,例如 CurrentItemChangedDataSourceChanged,它们允许进行自定义。Although currency management is handled automatically for all underlying data source types, this class exposes a number of events, such as CurrentItemChanged and DataSourceChanged, that allow for customization.

绑定到 BindingSource 组件的数据源还可以使用 BindingNavigator 类进行导航和管理,该类提供类似 VCR 的用户界面(UI)用于在列表中导航项。Data sources that are bound to a BindingSource component can also be navigated and managed with the BindingNavigator class, which provides a VCR-like user interface (UI) for navigating items within a list. 尽管 BindingNavigator 可以绑定到任何数据源,但它旨在通过其 BindingNavigator.BindingSource 属性与 BindingSource 组件集成。Although BindingNavigator can be bound to any data source, it was designed to integrate with a BindingSource component through its BindingNavigator.BindingSource property.

BindingSource 类的默认属性为 DataSourceThe default property for the BindingSource class is DataSource. 默认事件是 CurrentChangedThe default event is CurrentChanged.


BindingSource 类的许多成员对 List 属性所表示的基础列表进行操作,只需将其操作引用到基础列表。Many of the members of the BindingSource class operate on the underlying list represented by the List property and simply refer their operation to the underlying list. 因此,当 BindingSource 绑定到 IList的自定义实现时,这些成员的确切行为可能与类文档中所述的行为不同。Therefore, when the BindingSource is bound to a custom implementation of IList, the exact behavior of these members may differ from the behavior described in the class documentation. 例如,RemoveAt 方法调用 IList.RemoveAtFor example, the RemoveAt method calls IList.RemoveAt. BindingSource 文档介绍 RemoveAt 方法,并了解底层 IListRemoveAt 方法是否正确实现。The BindingSource documentation describes the RemoveAt method with the understanding that the RemoveAt method for the underlying IList is correctly implemented.



BindingSource 类的新实例初始化为默认属性值。Initializes a new instance of the BindingSource class to the default property values.


初始化 BindingSource 类的新实例,并将 BindingSource 添加到指定的容器。Initializes a new instance of the BindingSource class and adds the BindingSource to the specified container.

BindingSource(Object, String)

用指定的数据源和数据成员初始化 BindingSource 类的新实例。Initializes a new instance of the BindingSource class with the specified data source and data member.



获取一个值,该值指示是否可以编辑基础列表中的项。Gets a value indicating whether items in the underlying list can be edited.


获取或设置一个值,该值指示是否可以使用 AddNew() 方法向列表中添加项。Gets or sets a value indicating whether the AddNew() method can be used to add items to the list.


获取一个值,它指示是否可从基础列表中移除项。Gets a value indicating whether items can be removed from the underlying list.


获取一个指示组件是否可以引发事件的值。Gets a value indicating whether the component can raise an event.

(Inherited from Component)

获取包含 IContainerComponentGets the IContainer that contains the Component.

(Inherited from Component)

获取在的基础列表中项的总数。获取在基础列表中项的总数,考虑当前 Filter 值。Gets the total number of items in the underlying list, taking the current Filter value into consideration.


获取与此 BindingSource 关联的当前项管理器。Gets the currency manager associated with this BindingSource.


获取列表中的当前项。Gets the current item in the list.


获取或设置连接器当前绑定到的数据源中的特定列表。Gets or sets the specific list in the data source to which the connector currently binds to.


获取或设置连接器绑定到的数据源。Gets or sets the data source that the connector binds to.


获取一个值,用以指示 Component 当前是否处于设计模式。Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)

获取附加到此 Component 的事件处理程序的列表。Gets the list of event handlers that are attached to this Component.

(Inherited from Component)

获取或设置用于筛选查看哪些行的表达式。Gets or sets the expression used to filter which rows are viewed.


获取一个值,该值指示列表绑定是否已挂起。Gets a value indicating whether the list binding is suspended.


获取一个值,该值指示基础列表是否具有固定大小。Gets a value indicating whether the underlying list has a fixed size.


获取一个值,该值指示基础列表是否为只读。Gets a value indicating whether the underlying list is read-only.


获取一个值,该值指示是否可以对基础列表中的项排序。Gets a value indicating whether the items in the underlying list are sorted.


获取一个值,该值指示对集合的访问是否为同步的(线程安全)。Gets a value indicating whether access to the collection is synchronized (thread safe).


获取或设置指定索引处的列表元素。Gets or sets the list element at the specified index.


获取连接器绑定到的列表。Gets the list that the connector is bound to.


获取或设置基础列表中当前项的索引。Gets or sets the index of the current item in the underlying list.


获取或设置一个值,该值指示是否应引发 ListChanged 事件。Gets or sets a value indicating whether ListChanged events should be raised.


获取或设置 ComponentISiteGets or sets the ISite of the Component.

(Inherited from Component)

获取或设置用于排序的列名称以及用于查看数据源中的行的排序顺序。Gets or sets the column names used for sorting, and the sort order for viewing the rows in the data source.


获取应用于数据源的排序说明的集合。Gets the collection of sort descriptions applied to the data source.


获取列表中项的排序方向。Gets the direction the items in the list are sorted.


获取正在用于对列表进行排序的 PropertyDescriptorGets the PropertyDescriptor that is being used for sorting the list.


获取一个值,它指示数据源是否支持多列排序。Gets a value indicating whether the data source supports multi-column sorting.


获取一个值,它指示数据源是否支持更改通知。Gets a value indicating whether the data source supports change notification.


获取一个值,该值指示数据源是否支持筛选。Gets a value indicating whether the data source supports filtering.


获取一个值,它指示数据源是否支持使用 Find(PropertyDescriptor, Object) 方法进行搜索。Gets a value indicating whether the data source supports searching with the Find(PropertyDescriptor, Object) method.


获取一个值,它指示数据源是否支持排序。Gets a value indicating whether the data source supports sorting.


获取可用于同步对基础列表的访问的对象。Gets an object that can be used to synchronize access to the underlying list.



将现有项添加到内部列表中。Adds an existing item to the internal list.


在基础列表中添加一个新项。Adds a new item to the underlying list.


使用指定的排序说明对数据源进行排序。Sorts the data source with the specified sort descriptions.

ApplySort(PropertyDescriptor, ListSortDirection)

使用指定的属性说明符和排序方向对数据源进行排序。Sorts the data source using the specified property descriptor and sort direction.


取消当前的编辑操作。Cancels the current edit operation.


从列表中移除所有元素。Removes all elements from the list.


确定某个对象是否为列表中的项。Determines whether an object is an item in the list.

CopyTo(Array, Int32)

List 中的内容复制到指定数组,从指定索引值处开始。Copies the contents of the List to the specified array, starting at the specified index value.


创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)

释放 Component 使用的所有资源。Releases all resources used by the Component.

(Inherited from Component)

释放 BindingSource 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the BindingSource and optionally releases the managed resources.


将挂起的更改应用于基础数据源。Applies pending changes to the underlying data source.


确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Find(PropertyDescriptor, Object)

搜索具有指定属性描述符的项索引。Searches for the index of the item that has the given property descriptor.

Find(String, Object)

使用指定的属性名和值返回列表中的项的索引。Returns the index of the item in the list with the specified property name and value.


检索 List 的一个枚举数。Retrieves an enumerator for the List.


作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)

检索表示数据源列表类型的可绑定属性的 PropertyDescriptor 对象的数组。Retrieves an array of PropertyDescriptor objects representing the bindable properties of the data source list type.


检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)

获取为绑定提供数据的列表的名称。Gets the name of the list supplying data for the binding.


为指定的数据成员获取相关的当前项管理器。Gets the related currency manager for the specified data member.


返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)

搜索指定的对象,并返回整个列表中第一个匹配项的索引。Searches for the specified object and returns the index of the first occurrence within the entire list.


获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
Insert(Int32, Object)

将一项插入列表中指定的索引处。Inserts an item into the list at the specified index.


创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)

移至列表中的第一项。Moves to the first item in the list.


移至列表中的最后一项。Moves to the last item in the list.


移至列表中的下一项。Moves to the next item in the list.


移至列表中的上一项。Moves to the previous item in the list.


引发 AddingNew 事件。Raises the AddingNew event.


引发 BindingComplete 事件。Raises the BindingComplete event.


引发 CurrentChanged 事件。Raises the CurrentChanged event.


引发 CurrentItemChanged 事件。Raises the CurrentItemChanged event.


引发 DataError 事件。Raises the DataError event.


引发 DataMemberChanged 事件。Raises the DataMemberChanged event.


引发 DataSourceChanged 事件。Raises the DataSourceChanged event.


引发 ListChanged 事件。Raises the ListChanged event.


引发 PositionChanged 事件。Raises the PositionChanged event.


从列表中移除指定的项。Removes the specified item from the list.


移除此列表中指定索引处的项。Removes the item at the specified index in the list.


从列表中移除当前项。Removes the current item from the list.


移除与 BindingSource 关联的筛选器。Removes the filter associated with the BindingSource.


移除与 BindingSource 关联的排序。Removes the sort associated with the BindingSource.


重新初始化 AllowNew 属性。Reinitializes the AllowNew property.


使绑定到 BindingSource 的控件重新读取列表中的所有项,并刷新这些项的显示值。Causes a control bound to the BindingSource to reread all the items in the list and refresh their displayed values.


使绑定到 BindingSource 的控件重新读取当前选定的项,并刷新其显示值。Causes a control bound to the BindingSource to reread the currently selected item and refresh its displayed value.


使绑定到 BindingSource 的控件重新读取指定索引处的项,并刷新其显示值。Causes a control bound to the BindingSource to reread the item at the specified index, and refresh its displayed value.


继续数据绑定。Resumes data binding.


挂起数据绑定,以阻止使用所做的更改对绑定数据源进行更新。Suspends data binding to prevent changes from updating the bound data source.


返回包含 Component 的名称的 String(如果有)。Returns a String containing the name of the Component, if any. 不应重写此方法。This method should not be overridden.

(Inherited from Component)



在将项添加到基础列表之前发生。Occurs before an item is added to the underlying list.


当所有客户端都已绑定到此 BindingSource 时发生。Occurs when all the clients have been bound to this BindingSource.


在当前绑定项更改时发生。Occurs when the currently bound item changes.


Current 属性的属性值更改后发生。Occurs when a property value of the Current property has changed.


当货币相关的异常由 BindingSource 无提示处理时发生。Occurs when a currency-related exception is silently handled by the BindingSource.


DataMember 属性值更改后发生。Occurs when the DataMember property value has changed.


DataSource 属性值更改后发生。Occurs when the DataSource property value has changed.


在通过调用 Dispose() 方法释放组件时发生。Occurs when the component is disposed by a call to the Dispose() method.

(Inherited from Component)

当基础列表更改或列表中的项更改时发生。Occurs when the underlying list changes or an item in the list changes.


Position 属性的值更改后发生。Occurs after the value of the Position property has changed.

Explicit Interface Implementations


PropertyDescriptor 添加到用于搜索的索引。Adds the PropertyDescriptor to the indexes used for searching.


PropertyDescriptor 从用于搜索的索引中移除。Removes the PropertyDescriptor from the indexes used for searching.


丢弃集合中挂起的新项。Discards a pending new item from the collection.


向集合提交挂起的新项。Commits a pending new item to the collection.


用信号通知 BindingSource 初始化即将开始。Signals the BindingSource that initialization is starting.


用信号通知 BindingSource 初始化已完成。Signals the BindingSource that initialization is complete.


初始化 BindingSource 时出现。Occurs when the BindingSource is initialized.


获取一个值,该值指示是否初始化 BindingSourceGets a value indicating whether the BindingSource is initialized.

Extension Methods


IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.


根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.


启用查询的并行化。Enables parallelization of a query.


IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

Applies to

See also