- .NET Framework 4.5
- Visual Studio 2010
- Visual Studio 2008
- Visual Studio 2005
- .NET Framework 3.5
- .NET Framework 2.0
在 Windows 窗體中使用數據綁定時,經常有多個控件被綁定到同一數據源。在某些情況下,可能有必要採取額外步驟,以確保控件的綁定屬性彼此之間保持同步,並且與數據源保持同步。在兩種情況下,這些步驟是必要的:
-
如果數據源未實現 IBindingList,並因此生成類型 ItemChanged 的 ListChanged 事件。
-
如果數據源實現 IEditableObject。
在前一種情況下,可使用 BindingSource 將數據源綁定到控件。在後一種情況下,應使用 BindingSource 並處理 BindingComplete 事件,然後對關聯的 BindingManagerBase 調用 EndCurrentEdit。
示例
下面的代碼示例演示如何使用 BindingSource 組件,將三個控件(兩個文本框控件和一個 DataGridView 控件)綁定到 DataSet 中的同一列。該示例演示如何處理 BindingComplete 事件,並確保當一個文本框的文本值更改時,會用正確的值更新其他文本框和 DataGridView 控件。
該示例使用 BindingSource 來綁定數據源和控件。或者,可以直接將控件綁定到數據源,並從窗體的 BindingContext 檢索用於綁定的 BindingManagerBase,然後爲 BindingManagerBase 處理 BindingComplete 事件。有關如何進行此操作的示例,請參見 BindingManagerBase 的 BindingComplete 事件的相關幫助頁。
// Declare the controls to be used.
private BindingSource bindingSource1;
private TextBox textBox1;
private TextBox textBox2;
private DataGridView dataGridView1;
private void InitializeControlsAndDataSource()
{
// Initialize the controls and set location, size and
// other basic properties.
this.dataGridView1 = new DataGridView();
this.bindingSource1 = new BindingSource();
this.textBox1 = new TextBox();
this.textBox2 = new TextBox();
this.dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Dock = DockStyle.Top;
this.dataGridView1.Location = new Point(0, 0);
this.dataGridView1.Size = new Size(292, 150);
this.textBox1.Location = new Point(132, 156);
this.textBox1.Size = new Size(100, 20);
this.textBox2.Location = new Point(12, 156);
this.textBox2.Size = new Size(100, 20);
this.ClientSize = new Size(292, 266);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.dataGridView1);
// Declare the DataSet and add a table and column.
DataSet set1 = new 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);
bindingSource1.BindingComplete +=
new BindingCompleteEventHandler(bindingSource1_BindingComplete);
}
private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e)
{
// Check if the data source has been updated, and that no error has occured.
if (e.BindingCompleteContext ==
BindingCompleteContext.DataSourceUpdate && e.Exception == null)
// If not, end the current edit.
e.Binding.BindingManagerBase.EndCurrentEdit();
}