0

Does anyone know what event to hook into to know when data binding is complete on a ListBox control after setting its DataSource?

Hooking into DataSourceChanged doesn't do the trick as the Items collection is still empty on the control when this event is fired.

BigBytes
  • 339
  • 6
  • 19
  • The answer to my similar question might do the trick for you; see http://stackoverflow.com/questions/34421735/how-can-i-immediately-reactively-determine-if-any-checkedboxlistitem-has-been-se – B. Clay Shannon-B. Crow Raven Dec 22 '15 at 18:16

3 Answers3

1

The easy way is to just use the DataSource collection instead:

void listBox1_DataSourceChanged(object sender, EventArgs e) {
  var count = ((ICollection)listBox1.DataSource).Count;
}

Cast the DataSource appropriately.

Otherwise, you can use BeginInvoke to get the update after the DataSourceChanged event has run its course:

void listBox1_DataSourceChanged(object sender, EventArgs e) {
  this.BeginInvoke(new Action(() => {
    var count = listBox1.Items.Count;
  }));
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
1

Thanks guys. I actually decompiled the ListBox code using Resharper and found that the DisplayMemberChanged event did the trick for me so I used that. Thanks for the suggestions though!

BigBytes
  • 339
  • 6
  • 19
0

Does anyone know what event to hook into to know when data binding is complete on a ListBox control after setting its DataSource?

There is no such event. But you can create your own ListBox subclass and expose such event like this

public class MyListBox : ListBox
{
    public event EventHandler DataSourceApplied;

    protected override void OnDataSourceChanged(EventArgs e)
    {
        base.OnDataSourceChanged(e);
        var handler = DataSourceApplied;
        if (handler != null) handler(this, EventArgs.Empty);
    }
}
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343