Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>That view is fair enough. Under the covers, other objects such as CurrencyManager and Binding make sure controls are updated when the underlying data source changes.</p></li> <li><p>Adding an item to a data bound BindingList triggers a series of events that end up trying to update the DataGridView. Since the UI can only be updated from the UI thread, you should add items to BindingList from the UI thread through <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke.aspx" rel="nofollow noreferrer">Control.Invoke</a>.</p></li> </ol> <p>I assembled a quick sample creating a Form with a DataGridView, a BindingSource and a Button.</p> <p>The button spins up another thread that simulates getting a new item for inclusion in the BindingList. </p> <p>The inclusion itself is done back in the UI thread through Control.Invoke. </p> <pre><code> public partial class BindingListChangedForm : Form { BindingList&lt;Person&gt; people = new BindingList&lt;Person&gt;(); Action&lt;Person&gt; personAdder; public BindingListChangedForm() { InitializeComponent(); this.dataGridView1.AutoGenerateColumns = true; this.bindingSource1.DataSource = this.people; this.personAdder = this.PersonAdder; } private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(this.GotANewPersononBackgroundThread); t.Start(); } // runs on the background thread. private void GotANewPersononBackgroundThread() { Person person = new Person { Id = 1, Name = "Foo" }; //Invokes the delegate on the UI thread. this.Invoke(this.personAdder, person); } //Called on the UI thread. void PersonAdder(Person person) { this.people.Add(person); } } public class Person { public int Id { get; set; } public string Name { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload