Windows Forms에서 데이터 바인딩을 구현하는 동안 여러 컨트롤이 동일한 데이터 원본에 바인딩되는 경우가 많습니다. 다음 상황에서는 컨트롤의 바인딩된 속성이 서로 및 데이터 원본과 동기화된 상태로 유지되도록 해야 합니다.
데이터 원본이 IBindingList을(를) 구현하지 않는 경우 ItemChanged 형식의 ListChanged 이벤트를 생성합니다.
데이터 원본이 IEditableObject를 구현하는 경우.
전자의 경우 BindingSource를 사용하여 데이터 원본을 컨트롤에 바인딩할 수 있습니다. 후자의 경우 BindingSource
를 사용하고 BindingComplete 이벤트를 처리하고 연결된 BindingManagerBase에서 EndCurrentEdit를 호출합니다.
BindingSource를 사용하는 바인딩 컨트롤의 예
다음 코드 예제에서는 BindingSource 구성 요소를 사용하여 DataSet의 동일한 열에 3개의 컨트롤(두 개의 텍스트 상자 컨트롤과 DataGridView 컨트롤)을 바인딩하는 방법을 보여 줍니다. BindingComplete 이벤트를 처리하는 방법을 설명하는 예제. 한 텍스트 상자의 텍스트 값이 변경되면 다른 텍스트 상자와 DataGridView
컨트롤이 올바른 값으로 업데이트됩니다.
이 예제에서는 BindingSource를 사용하여 데이터 원본과 컨트롤을 바인딩합니다. 또는 컨트롤을 데이터 원본에 직접 바인딩하고 양식의 BindingContext에서 바인딩에 대한 BindingManagerBase를 검색한 다음 BindingManagerBase
에 대한 BindingComplete 이벤트를 처리할 수 있습니다. 데이터 원본 및 컨트롤을 바인딩하는 방법에 대한 자세한 내용은 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
참고하십시오
.NET Desktop feedback