Windows フォームでのデータ バインディングの実装時に、複数のコントロールが同じデータ ソースにバインドされます。 次の状況では、コントロールのバインドされたプロパティが互いに同期され、データ ソースが維持されるようにする必要があります。
データ ソースがIBindingListを実装していないため、ListChanged型のItemChangedイベントが生成される場合。
データ ソースが IEditableObjectを実装している場合。
前者の場合は、BindingSource を使用してデータ ソースをコントロールにバインドできます。 後者の場合は、BindingSource
を使用して BindingComplete イベントを処理し、関連付けられている EndCurrentEditに対して BindingManagerBase を呼び出します。
BindingSource を使用したバインド コントロールの例
次のコード例では、DataGridView コンポーネントを使用して、3 つのコントロール、2 つのテキスト ボックス コントロール、およびDataSet コントロールをBindingSource内の同じ列にバインドする方法を示します。 この例では、 BindingComplete イベントを処理する方法を示します。 これにより、1 つのテキスト ボックスのテキスト値が変更されると、もう一方のテキスト ボックスと DataGridView
コントロールが正しい値で更新されます。
この例では、 BindingSource を使用してデータ ソースとコントロールをバインドします。 または、コントロールをデータ ソースに直接バインドし、フォームのBindingManagerBaseからバインドのBindingContextを取得し、BindingCompleteのBindingManagerBase
イベントを処理することもできます。 データ ソースとコントロールのバインドの詳細については、BindingComplete
のBindingManagerBase
イベントに関するヘルプ ページを参照してください。
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
こちらも参照ください
.NET Desktop feedback