BindingSource 類別

定義

封裝表單的資料來源。

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
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
[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
[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")>]
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
[<System.ComponentModel.ComplexBindingProperties("DataSource", "DataMember")>]
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
Public Class BindingSource
Inherits Component
Implements IBindingListView, ICancelAddNew, ICurrencyManagerProvider, IList, ISupportInitializeNotification, ITypedList
Public Class BindingSource
Inherits Component
Implements IBindingListView, ICancelAddNew, ICurrencyManagerProvider, IList, ISupportInitialize, ISupportInitializeNotification, ITypedList
繼承
屬性
實作

範例

下列程式碼範例示範 ListBox 系結至 的 BindingSourceBindingSource系結至 BindingList<T> 包含字型清單的 。

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
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            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);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.listBox1);

            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.");
            }
            else
            {
                int foundIndex = binding1.Find("Name", textBox1.Text);
                if (foundIndex > -1)
                    listBox1.SelectedIndex = foundIndex;
                else
                    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.EnableVisualStyles()
        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)
        Me.Controls.Add(Me.button1)
        Me.Controls.Add(Me.textBox1)
        Me.Controls.Add(Me.listBox1)

        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.")
        Else
            Dim foundIndex As Integer = binding1.Find("Name", textBox1.Text)
            If foundIndex > -1 Then
                listBox1.SelectedIndex = foundIndex
            Else
                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
        Get
            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 有許多用途。 首先,它藉由在Windows Forms控制項和資料來源之間提供貨幣管理、變更通知和其他服務,來簡化表單上的系結控制項。 這可藉由使用 DataSource 屬性將 BindingSource 元件附加至資料來源來完成。 對於複雜的系結案例,您可以選擇性地將 DataMember 屬性設定為數據源中的特定資料行或清單。 然後,您將控制項系結至 BindingSource 。 所有與資料的進一步互動都是透過呼叫 BindingSource 元件來完成。 如需如何簡化系結程式的範例,請參閱如何 BindingSource :將控制項系結Windows Forms DBNull 資料庫值以及如何:處理資料系結所發生的錯誤和例外狀況。 透過 、 MoveLast 和 等 MoveNext 方法完成資料來源的流覽和 Remove 更新。 排序和篩選等作業會透過 SortFilter 屬性來處理。 如需搭配 使用排序和篩選 BindingSource 的詳細資訊,請參閱How to: Sort and Filter ADO.NET Data with the Windows Forms BindingSource Component

此外, BindingSource 元件也可以做為強型別資料來源。 基礎資料來源的類型通常是透過下列其中一種機制來修正:

這兩種機制都會建立強型別清單。 如需如何使用 BindingSource 系結至類型的詳細資訊,請參閱如何:將Windows Forms控制項系結至類型。 您也可以使用 BindingSource 將控制項系結至 Factory 物件。 如需如何執行這項操作的詳細資訊,請參閱如何:將Windows Forms控制項系結至 Factory 物件

注意

因為處理 BindingSource 簡單和複雜的資料來源,所以術語有問題。 在此類別檔中, 字詞清單 是指託管資料來源內的資料收集,而 專案 表示單一元素。 討論與複雜資料來源相關聯的功能時,會使用對等詞彙 資料表 和資料

BindingSource 提供成員來存取基礎資料。 目前的專案可以透過 Current 屬性擷取,而整個清單可以透過 List 屬性擷取。 透過 和 RemoveCurrentEndEditCancelEdit 和 方法 AddAddNew ,目前專案 Current 支援編輯作業。 雖然所有基礎資料來源類型都會自動處理貨幣管理,但此類別會公開許多事件,例如 CurrentItemChangedDataSourceChanged ,以允許自訂。

系結至 BindingSource 元件的資料來源也可以透過 BindingNavigator 類別來巡覽及管理,此類別提供類似 VCR 的使用者介面 (UI) ,以巡覽清單中的專案。 雖然 BindingNavigator 可以系結至任何資料來源,但它的設計目的是透過其 BindingNavigator.BindingSource 屬性與 BindingSource 元件整合。

類別的預設屬性 BindingSourceDataSource 。 預設事件為 CurrentChanged

警告

類別的許多 BindingSource 成員都會在 屬性所 List 代表的基礎清單上運作,並直接將其作業參考基礎清單。 因此,當 系結至 的 IList 自訂實作時 BindingSource ,這些成員的確切行為可能與類別檔中所述的行為不同。 例如,方法會 RemoveAt 呼叫 IList.RemoveAt 。 檔 BindingSource 描述 RemoveAt 方法,瞭解 RemoveAt 基礎 IList 的方法已正確實作。

建構函式

BindingSource()

初始化 BindingSource 類別的新執行個體成為預設屬性值。

BindingSource(IContainer)

初始化 BindingSource 類別的新執行個體,並將 BindingSource 加入指定的容器中。

BindingSource(Object, String)

使用指定的資料來源和資料成員,初始化 BindingSource 類別的新執行個體。

屬性

AllowEdit

取得值,指出基礎清單中的項目是否可以編輯。

AllowNew

取得或設定值,指出 AddNew() 方法是否可以用來將項目加入清單中。

AllowRemove

取得值,指出是否可以從基礎清單中移除項目。

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Container

取得包含 IContainerComponent

(繼承來源 Component)
Count

將目前的 Filter 值納入考慮,取得基本清單中的項目總數。

CurrencyManager

取得與這個 BindingSource 關聯的 Currency 管理員。

Current

取得清單中的目前項目。

DataMember

取得或設定此連接器目前繫結至的資料來源中的特定清單。

DataSource

取得或設定此連結器所繫結至的資料來源。

DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
Filter

取得或設定用來篩選檢視的資料列的運算式。

IsBindingSuspended

取得值,指出清單繫結是否已暫止。

IsFixedSize

取得值,指出基礎清單是否有固定的大小。

IsReadOnly

取得值,指出基礎清單是否為唯讀。

IsSorted

取得值,指出基礎清單中的項目是否已排序。

IsSynchronized

取得值,表示是否同步化存取集合 (執行緒安全)。

Item[Int32]

取得或設定指定索引中的清單項目。

List

取得連接器要繫結至其中的清單。

Position

取得或設定基礎清單中目前項目的索引。

RaiseListChangedEvents

取得或設定值,指出是否應該引發 ListChanged 事件。

Site

取得或設定 ComponentISite

(繼承來源 Component)
Sort

取得或設定用來排序的資料行名稱,以及用來檢視資料來源中資料列的排序次序。

SortDescriptions

取得套用至資料來源的排序描述集合。

SortDirection

取得清單中項目排序的方向。

SortProperty

取得已經用來為清單排序的 PropertyDescriptor

SupportsAdvancedSorting

取得值,指出資料來源是否支援多欄排序。

SupportsChangeNotification

取得值,指出資料來源是否支援變更告知。

SupportsFiltering

取得值,指出資料來源是否支援篩選。

SupportsSearching

取得值,指出資料來源是否支援使用 Find(PropertyDescriptor, Object) 方法進行搜尋。

SupportsSorting

取得值,指出資料來源是否支援排序。

SyncRoot

取得可用來同步對基礎清單存取的物件。

方法

Add(Object)

將現有項目加入內部清單中。

AddNew()

將新的項目加入基礎清單中。

ApplySort(ListSortDescriptionCollection)

使用指定的排序描述,對資料來源排序。

ApplySort(PropertyDescriptor, ListSortDirection)

使用指定的屬性描述項和排序方向,對資料來源排序。

CancelEdit()

取消目前的編輯作業。

Clear()

將所有項目從清單中移除。

Contains(Object)

判斷某個物件是否為清單中的項目。

CopyTo(Array, Int32)

從指定的索引值開始,將 List 的內容複製至指定的陣列。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 BindingSource 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

EndEdit()

將暫止的變更套用至基礎資料來源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Find(PropertyDescriptor, Object)

搜尋具有指定屬性描述項的項目之索引。

Find(String, Object)

傳回清單中具有指定屬性名稱和值的項目之索引。

GetEnumerator()

擷取 List 的列舉值。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetItemProperties(PropertyDescriptor[])

擷取 PropertyDescriptor 物件的陣列,表示資料來源清單型別的可繫結屬性。

GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetListName(PropertyDescriptor[])

取得為繫結提供資料的清單名稱。

GetRelatedCurrencyManager(String)

取得指定之資料成員的相關 Currency 管理員。

GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(Object)

搜尋指定的物件,並傳回整個清單中第一個相符項目的索引。

InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
Insert(Int32, Object)

將項目插入位於指定索引的清單中。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
MoveFirst()

移至清單中的第一個項目。

MoveLast()

移至清單中的最後一個項目。

MoveNext()

移至清單中的下一個項目。

MovePrevious()

移至清單中的上一個項目。

OnAddingNew(AddingNewEventArgs)

引發 AddingNew 事件。

OnBindingComplete(BindingCompleteEventArgs)

引發 BindingComplete 事件。

OnCurrentChanged(EventArgs)

引發 CurrentChanged 事件。

OnCurrentItemChanged(EventArgs)

引發 CurrentItemChanged 事件。

OnDataError(BindingManagerDataErrorEventArgs)

引發 DataError 事件。

OnDataMemberChanged(EventArgs)

引發 DataMemberChanged 事件。

OnDataSourceChanged(EventArgs)

引發 DataSourceChanged 事件。

OnListChanged(ListChangedEventArgs)

引發 ListChanged 事件。

OnPositionChanged(EventArgs)

引發 PositionChanged 事件。

Remove(Object)

從清單中移除指定的項目。

RemoveAt(Int32)

移除清單中位於指定索引上的項目。

RemoveCurrent()

從清單中移除目前項目。

RemoveFilter()

移除與 BindingSource 有關的篩選條件。

RemoveSort()

移除與 BindingSource 有關的排序。

ResetAllowNew()

重新初始化 AllowNew 屬性。

ResetBindings(Boolean)

使得繫結至 BindingSource 的控制項重新讀取清單中的所有項目,並重新整理其顯示的值。

ResetCurrentItem()

使得繫結至 BindingSource 的控制項重新讀取目前選取的項目,並重新整理其顯示的值。

ResetItem(Int32)

使得繫結至 BindingSource 的控制項重新讀取指定索引上的項目,並重新整理其顯示的值。

ResumeBinding()

繼續資料繫結。

SuspendBinding()

暫止資料繫結以防止變更更新繫結的資料來源。

ToString()

傳回任何包含 Component 名稱的 String。 不應覆寫此方法。

(繼承來源 Component)

事件

AddingNew

在項目加入基礎清單之前發生。

BindingComplete

發生於所有的用戶端都已繫結至這個 BindingSource 時。

CurrentChanged

發生於目前繫結的項目變更時。

CurrentItemChanged

發生於 Current 屬性的屬性值已變更時。

DataError

BindingSource 以無訊息模式處理與貨幣有關的例外狀況時發生。

DataMemberChanged

發生於 DataMember 屬性值變更時。

DataSourceChanged

發生於 DataSource 屬性值變更時。

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)
ListChanged

發生於基礎清單有變更或清單中的項目有變更時。

PositionChanged

發生於 Position 屬性的值已變更後。

明確介面實作

IBindingList.AddIndex(PropertyDescriptor)

PropertyDescriptor 加入用來搜尋的索引中。

IBindingList.RemoveIndex(PropertyDescriptor)

從用來搜尋的索引中移除 PropertyDescriptor

ICancelAddNew.CancelNew(Int32)

從集合中捨棄暫止的新項目。

ICancelAddNew.EndNew(Int32)

將暫止的新項目認可到集合中。

ISupportInitialize.BeginInit()

BindingSource 表示正在啟動初始化。

ISupportInitialize.EndInit()

BindingSource 表示初始化已完成。

ISupportInitializeNotification.Initialized

發生於初始化 BindingSource 時。

ISupportInitializeNotification.IsInitialized

取得值,指出是否已初始化 BindingSource

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱