Now I want to bind this kick-ass custom collection to my form's DataGridView. Here's the solution I've come up with so far.
private void BindDataSource()
{
BindingSource bs = new BindingSource();
bs.DataSource = this.myConnection.CustomCollection;
if (!this.dgvCot.InvokeRequired)
{
this.dgvCot.DataSource = bs;
}
else
{
this.dgvCot.BeginInvoke(new MethodInvoker(delegate() {
this.dgvCot.DataSource = bs; }));
}
}
This works but as you can probably guess this code is being called from another thread, hence all of the BeginInvoke and InvokeRequired stuff in there. Specifically, this is part of an event handler that is fired every time I receive some data that is serialized into mycustomclass and then added to the custom collection.
I would think that I shouldn't have to bind the collection every time something is added to the custom collection. But I can't get the DataGridView to update if the binding code is not in this event.
I've tried inheriting from BindingList<> but its behavior is kind of strange. The DataGridView will only show one of the objects in the collection and never show anymore.
I guess it will come to me.
No comments:
Post a Comment