470,819 Members | 1,630 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,819 developers. It's quick & easy.

ListBox Populating Question

I populate a ListBox with a LogFile that has about (~1000 lines). The
ListBox's datasource is a BindingList<string>. Whenever I add the
elements, with the datasource set, it takes about 2 mins.

I've tried wrapping "Listbox.SuspendLayout()" and
"Listbox.ResumeLayout()" around the for loop (that does the adding)
but it still takes about 2 mins. Only thing I can seem to do is set
the DataSource to null, add to the BindingList and then reset the
DataSource (this way the ListBox is populated in about 1 second or
less)... Which, I think, defeats the purpose of the BindingList?

How do I tell the ListBox (or the BindingList) to temporarly stop
updating its bindings? Then I can just call the ".ResetBindings()"
function after I'm done adding to the BindingList.

Thanks
NB
Jan 15 '08 #1
2 3699
Well, if you still a BindingSource between the ListBox and the
BindingList<T>, then you can simply suspend the BindingSource:

bindingSource1.DataSource = list;
listbox1.DataSource = bindingSource1;
// ...
bindingSource1.SuspendBinding();
// ...
bindingSource1.ResumeBinding();
// (might also need bindingSource1.ResetBindings(false);
// but see how it goes without first)

Alternatively, you can just tell the list itself to shut up for a
while:

bool wasEnabled = list.RaiseListChangedEvents;
list.RaiseListChangedEvents = false;
try
{
// ...
}
finally
{
if (wasEnabled) // then re-enable it and force a
refresh
{
list.RaiseListChangedEvents = true;
list.ResetBindings();
}
}

That do?

Marc
Jan 16 '08 #2
On Jan 15, 11:49*pm, Marc Gravell <marc.grav...@gmail.comwrote:
Well, if you still a BindingSource between the ListBox and the
BindingList<T>, then you can simply suspend the BindingSource:

* * * * * * bindingSource1.DataSource = list;
* * * * * * listbox1.DataSource = bindingSource1;
* * * * * * // ...
* * * * * * bindingSource1.SuspendBinding();
* * * * * * // ...
* * * * * * bindingSource1.ResumeBinding();
* * * * * * // (might also need bindingSource1.ResetBindings(false);
* * * * * * // but see how it goes without first)

Alternatively, you can just tell the list itself to shut up for a
while:

* * * * * * bool wasEnabled = list.RaiseListChangedEvents;
* * * * * * list.RaiseListChangedEvents = false;
* * * * * * try
* * * * * * {
* * * * * * * * // ...
* * * * * * }
* * * * * * finally
* * * * * * {
* * * * * * * * if (wasEnabled) // then re-enable it and force a
refresh
* * * * * * * * {
* * * * * * * * * * list.RaiseListChangedEvents = true;
* * * * * * * * * * list.ResetBindings();
* * * * * * * * }
* * * * * * }

That do?

Marc
Ahh, Thank you kindly. Both methods you mentioned works perfectly :)

NB
Jan 16 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike P | last post: by
3 posts views Thread by Paul W | last post: by
6 posts views Thread by Chris Leuty | last post: by
4 posts views Thread by Henrik Holle | last post: by
6 posts views Thread by Janaka | last post: by
3 posts views Thread by joe | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.