Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Durante la implementación del enlace de datos en Formularios Windows Forms, varios controles se enlazan al mismo origen de datos. En las situaciones siguientes, es necesario asegurarse de que las propiedades enlazadas del control permanecen sincronizadas entre sí y con el origen de datos:
Si el origen de datos no implementa IBindingListy, por tanto, genera ListChanged eventos de tipo ItemChanged.
Si el origen de datos implementa IEditableObject.
En el primer caso, puede utilizar un BindingSource para enlazar el origen de datos a los controles. En este último caso, se utiliza un BindingSource
y se maneja el evento BindingComplete y se llama a EndCurrentEdit en el BindingManagerBase asociado.
Ejemplo de controles de enlace mediante BindingSource
En el ejemplo de código siguiente se muestra cómo enlazar tres controles, dos controles de cuadro de texto y un DataGridView control a la misma columna de un DataSet mediante un BindingSource componente. En el ejemplo se muestra cómo controlar el BindingComplete evento. Garantiza que cuando se cambia el valor de texto de un cuadro de texto, el otro cuadro de texto y el DataGridView
control se actualizan con el valor correcto.
En el ejemplo se usa un BindingSource para enlazar el origen de datos y los controles. Como alternativa, puede enlazar los controles directamente al origen de datos y recuperar el BindingManagerBase del enlace desde el formulario BindingContext, y luego manejar el evento BindingComplete para el BindingManagerBase
. Para obtener más información sobre cómo enlazar el origen de datos y los controles, vea la página de ayuda sobre el BindingComplete
evento de 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
Consulte también
.NET Desktop feedback