Synchronizowanie wielu kontrolek z tym samym źródłem danych (Windows Forms .NET)
Podczas implementacji powiązania danych w formularzach Systemu Windows wiele kontrolek jest powiązanych z tym samym źródłem danych. W następujących sytuacjach należy upewnić się, że powiązane właściwości kontrolki pozostają zsynchronizowane ze sobą i ze źródłem danych:
Jeśli źródło danych nie implementuje IBindingListelementu , a zatem generuje ListChanged zdarzenia typu ItemChanged.
Jeśli źródło danych implementuje IEditableObjectelement .
W poprzednim przypadku można użyć elementu , BindingSource aby powiązać źródło danych z kontrolkami. W tym drugim przypadku należy użyć BindingSource
elementu i obsłużyć BindingComplete zdarzenie i wywołać EndCurrentEdit skojarzone BindingManagerBasepolecenie .
Przykład kontrolek powiązania przy użyciu elementu BindingSource
W poniższym przykładzie kodu pokazano, jak powiązać trzy kontrolki, dwie kontrolki pola tekstowe i kontrolkę DataGridView z tą samą kolumną w elemencie DataSet używającym BindingSource składnika. W przykładzie pokazano, jak obsługiwać BindingComplete zdarzenie. Gwarantuje to, że po zmianie wartości tekstowej jednego pola tekstowego drugie pole tekstowe i kontrolka DataGridView
zostaną zaktualizowane o poprawną wartość.
W przykładzie użyto kontrolki , BindingSource aby powiązać źródło danych i kontrolki. Alternatywnie możesz powiązać kontrolki bezpośrednio ze źródłem danych i pobrać BindingManagerBase element dla powiązania z formularza BindingContext , a następnie obsłużyć BindingComplete zdarzenie dla BindingManagerBase
elementu . Aby uzyskać więcej informacji na temat powiązania źródła danych i kontrolek, zobacz stronę pomocy dotyczącą BindingComplete
zdarzenia .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
Zobacz też
.NET Desktop feedback
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla