usar ejemplo data control como c# .net data-binding bindingsource

ejemplo - data binding c#



DataBinding de DataGridView y List<> con BindingSource (2)

List<T> no admite cambios de eventos; BindingList<T> sería un buen sustituto para admitir este escenario, y también admite eventos de cambio a nivel de artículo si su tipo T implementa INotifyPropertyChanged .

En 3.0 y superior, también hay ObservableCollection<T> , que actúa de forma similar a BindingList<T> . Todo se reduce a interfaces como IBindingList , IBindingListView , etc.

De comentarios; para un ejemplo 2.0 / 3.0 de agregar un Find a BindingList<T> :

public class MyBindingList<T> : BindingList<T> { public T Find(Predicate<T> predicate) { if (predicate == null) throw new ArgumentNullException("predicate"); foreach (T item in this) { if (predicate(item)) return item; } return default(T); } }

Tenga en cuenta que en 3.5 (o en .NET 2.0 / 3.0 con LINQBridge y C # 3.0) no necesita esto: cualquiera de los métodos de extensión LINQ haría lo mismo.

Estoy tratando de descubrir cómo se supone que funciona el enlace de datos con BindingSource se BindingSource un DataGridView con el contenido de una List<> al momento de la actualización de la lista.

Puedo ver crecer la List y verificar que se llene cuando consulto el depurador. Pensé que BindingSource un evento cuando se cambiara la List . Pero ninguno de los disponibles está despedido. ¿Cómo me notifican cuando se modifica la lista subyacente?

Sigo las instrucciones y tengo el siguiente código de prueba:

Data d; BindingSource bs; public Form1() { InitializeComponent(); bs = new BindingSource(); d = new Data(); } private void Form1_Load(object sender, EventArgs e) { bs.DataSourceChanged += new EventHandler(bs_DataSourceChanged); bs.ListChanged += new ListChangedEventHandler(bs_ListChanged); bs.DataMemberChanged += new EventHandler(bs_DataMemberChanged); bs.CurrentChanged += new EventHandler(bs_CurrentChanged); bs.CurrentItemChanged += new EventHandler(bs_CurrentItemChanged); bs.DataSource = d.list; dataGridView1.DataSource = bs; } // ... all the handling methods caught with a break point in VS. private void button1_Click(object sender, EventArgs e) { d.addOneItem(); }


Si desea recibir una notificación cuando se cambie un inmueble, deberá implementar INotifyPropertyChanged

Vea aquí para un ejemplo.