相关文章推荐
鬼畜的椰子  ·  Docker ...·  5 月前    · 
豪气的双杠  ·  实现两列布局的 6 ...·  1 年前    · 

在實作Windows Forms中的資料系結期間,多個控制項會系結至相同的資料來源。 在下列情況下,請務必確保控制項的系結屬性會彼此和資料來源保持同步:

  • 如果資料來源未實 IBindingList 作 ,因此會產生 ListChanged 類型的 ItemChanged 事件。

  • 如果資料來源實作 ,則為 IEditableObject

    在先前的案例中,您可以使用 BindingSource 將資料來源系結至控制項。 在後者的情況下,您會使用 BindingSource 並處理 事件, BindingComplete 並在相關聯的 BindingManagerBase 上呼叫 EndCurrentEdit

    使用 BindingSource 系結控制項的範例

    下列程式碼範例示範如何使用 元件,將三個控制項、兩個 DataGridView 文字方塊控制項和控制項系結至 中的 DataSet BindingSource 相同資料行。 此範例示範如何處理 BindingComplete 事件。 它可確保當一個文字方塊的文字值變更時,另一個文字方塊和 DataGridView 控制項會以正確的值更新。

    此範例使用 BindingSource 來系結資料來源和控制項。 或者,您可以將控制項直接系結至資料來源,並從表單的 BindingContext 中擷取 BindingManagerBase 系結的 ,然後處理 BindingComplete BindingManagerBase 事件。 如需系結資料來源和控制項的詳細資訊,請參閱 有關 事件 BindingManagerBase 的說明頁面 BindingComplete

    public Form1() InitializeComponent(); set1.Tables.Add("Menu"); set1.Tables[0].Columns.Add("Beverages"); // Add some rows to the table. set1.Tables[0].Rows.Add("coffee"); set1.Tables[0].Rows.Add("tea"); set1.Tables[0].Rows.Add("hot chocolate"); set1.Tables[0].Rows.Add("milk"); set1.Tables[0].Rows.Add("orange juice"); // Set the data source to the DataSet. bindingSource1.DataSource = set1; //Set the DataMember to the Menu table. bindingSource1.DataMember = "Menu"; // Add the control data bindings. dataGridView1.DataSource = bindingSource1; textBox1.DataBindings.Add("Text", bindingSource1, "Beverages", true, DataSourceUpdateMode.OnPropertyChanged); textBox2.DataBindings.Add("Text", bindingSource1, "Beverages", true, DataSourceUpdateMode.OnPropertyChanged); bindingSource1.BindingComplete += new BindingCompleteEventHandler(bindingSource1_BindingComplete); void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e) // Check if the data source has been updated, and that no error has occurred. if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate && e.Exception == null) // If not, end the current edit. e.Binding.BindingManagerBase.EndCurrentEdit(); Public Class Form1 Private Sub InitializeControlsAndDataSource() ' Add a table and column to DataSet. set1.Tables.Add("Menu") set1.Tables(0).Columns.Add("Beverages") ' Add some rows to the table. set1.Tables(0).Rows.Add("coffee") set1.Tables(0).Rows.Add("tea") set1.Tables(0).Rows.Add("hot chocolate") set1.Tables(0).Rows.Add("milk") set1.Tables(0).Rows.Add("orange juice") ' Set the data source to the DataSet. BindingSource1.DataSource = set1 'Set the DataMember to the Menu table. BindingSource1.DataMember = "Menu" ' Add the control data bindings. DataGridView1.DataSource = BindingSource1 TextBox1.DataBindings.Add("Text", BindingSource1, "Beverages", True, DataSourceUpdateMode.OnPropertyChanged) TextBox2.DataBindings.Add("Text", BindingSource1, "Beverages", True, DataSourceUpdateMode.OnPropertyChanged) End Sub Private Sub BindingSource1_BindingComplete(ByVal sender As Object, ByVal e As BindingCompleteEventArgs) Handles BindingSource1.BindingComplete ' Check if the data source has been updated, and that no error has occurred. If e.BindingCompleteContext = BindingCompleteContext.DataSourceUpdate _ AndAlso e.Exception Is Nothing Then ' If not, end the current edit. e.Binding.BindingManagerBase.EndCurrentEdit() End If End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load InitializeControlsAndDataSource() End Sub End Class
  • 使用變更通知設計絕佳的資料來源
  • 作法:使用 BindingSource 元件跨表單共用繫結資料
  •