BindingSource.ResetBindings(Boolean) 方法


使得繫結至 BindingSource 的控制項重新讀取清單中的所有項目,並重新整理其顯示的值。Causes a control bound to the BindingSource to reread all the items in the list and refresh their displayed values.

 void ResetBindings(bool metadataChanged);
public void ResetBindings (bool metadataChanged);
member this.ResetBindings : bool -> unit
Public Sub ResetBindings (metadataChanged As Boolean)



如果資料結構描述已變更,則為 true;如果只是變更值,則為 falsetrue if the data schema has changed; false if only values have changed.


下列程式碼範例會使用 BindingSource 元件來系結陣列清單,而不會提供變更通知。The following code example uses a BindingSource component to bind an array list, which does not provide change notification. 專案會從清單中移除,而繫結控制項則會藉由呼叫方法來通知變更 ResetBindingsAn item is removed from the list, and the bound controls are notified of the change by calling the ResetBindings method. 這個程式碼範例是 如何:使用 BindingSource 反映 Windows Forms 控制項中的資料來源更新所提供之較大範例的一部分。This code example is part of a larger example provided in How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource.

   void button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
      String^ xml = "<US><states>"
         + "<state><name>Washington</name><capital>Olympia</capital> "
         + "<flower>Coast Rhododendron</flower></state>"
         + "<state><name>Oregon</name><capital>Salem</capital>"
         + "<flower>Oregon Grape</flower></state>"
         + "<state><name>California</name><capital>Sacramento</capital>"
         + "<flower>California Poppy</flower></state>"
         + "<state><name>Nevada</name><capital>Carson City</capital>"
         + "<flower>Sagebrush</flower></state>"
         + "</states></US>";
      // Convert the xml string to bytes and load into a memory stream.
      array<Byte>^ xmlBytes = Encoding::UTF8->GetBytes( xml );
      MemoryStream^ stream = gcnew MemoryStream( xmlBytes,false );
      // Create a DataSet and load the xml into it.
      dataSet2->ReadXml( stream );
      // Set the data source.
      bindingSource1->DataSource = dataSet2;
      bindingSource1->ResetBindings( true );
private void button1_Click(object sender, EventArgs e)
    // If items remain in the list, remove the first item. 
    if (states.Count > 0)

        // Call ResetBindings to update the textboxes.

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

    ' If items remain in the list, remove the first item. 
    If states.Count > 0 Then

        ' Call ResetBindings to update the textboxes.
    End If

End Sub


ResetBindings方法會通知所有系結至的控制項 BindingSource ,以重新整理其值。The ResetBindings method informs all controls bound to the BindingSource to refresh their values. 方法會引發 ListChanged 事件至少一次,而 metaDataChanged 參數會指出基礎變更的本質。The method does this by raising the ListChanged event at least once; the metaDataChanged parameter indicates the nature of the underlying change.

無論的值為何 metaDataChangedListChanged 都會 ListChangedEventArgs.ListChangedType 將設定為來引發事件 ListChangedType.ResetRegardless of the value of metaDataChanged, a ListChanged event is raised with ListChangedEventArgs.ListChangedType set to ListChangedType.Reset. 因此, ResetBindings 使用的參數呼叫 true 將會引發兩個 ListChanged 事件。As a consequence, calling ResetBindings with a parameter of true will raise two ListChanged events.

ResetBindings 當另一個成員對資料系結進行重大變更(例如設定或屬性)時,會自動呼叫 DataSource DataMemberResetBindings is automatically called whenever another member makes major changes to the data-binding, such as setting the DataSource or DataMember properties. 不過,程式設計人員也可以明確地呼叫這個方法。However, the programmer can also call this method explicitly.