Some Quick Questions

N

NvrBst

A few quick questions if possible.

1 - If I have a "List<..> myList" and set it as a datasouce for say
ListBox1, then change items in "myList" is the correct/only way to
update the ListBox with the following?

ListBox1.DataSource = null;
ListBox1.DataSource = myList;

2 - BindingList<...> is the same as List<...> except when it is set as
a datasource then it auto updates the ListBox on changes (One Way
Only) basically? Should I pretty much always be using
BindingList<...> if its being used as a datasouce and items are
changing? Is there an easy way to search MSDN for all Data Storage
Types that AutoUpdate when bound to a DataSource or is this basically
the only one?

3 - When Using the foreach enumeration I always seem to run into
"collection can't change" exception, and then forced to copy the
elements I want using the "CopyTo(..)" (of if I'm lucky and the
collection has it - ToArray()), or just use the simple
"for(...)" (Which is what I usally do since I don't like using up the
extra memory with the copy/toarray). Basically wondering if I am
doing this the right way or is there something else that the
getEnumeration makers put in place for me in these situations.


Thanks. I know these are probably really basic questions but I run
into them so often and just want to make sure I'm doing it the best
way I can.
 
M

Marc Gravell

1: Well, if there is a BindingSource in the mix you could call
ResetBindings(), but otherwise yes

2: Anything that provides an IBindingList implementation with support
for notifications (ListChanged) will suffice. BindingList<T> (for
classes) and DataView (for DataTables) are the most common two, but
you can write your own from scratch etc (if you feel so inclined). You
can also subclass BindingList<T> to fill in the blanks (sorting
support, etc).

3: What are you trying to do to the collection? List<T> (but not
BindingList<T>) offers various delegate-based methods for removing
multiple items etc - but it is right: if the list contents change, the
enumerator will break. A standard for(int i ...) will suffice - but
again, it depends on what you want to do.

Marc
 
M

Marc Gravell

Is there an easy way to search MSDN for all Data Storage
Types that AutoUpdate when bound to a DataSource or is this basically
the only one?

Well, the hard part is knowing that IBindingList is responsible for
list updates... MSDN used to offer a "things that derive from this",
but not any more - so to explain how I (a few moments ago) checked
this: I fired up the free but excellent "Lutz Roeder's .NET
Reflector", searched for IBindingList and expanded Derived Types. This
revealed:

System.ComponentModel.BindingList<T>
System.ComponentModel.IBindingListView
System.Data.DataView
System.Data.RelatedView
System.Windows.Forms.BindingSource
System.Data.DataViewManager

So the only other thing (in the standard libraries) that offers this
is BindingSource, which is actually just an intermediary for working
with a concrete implementation (like BindingList<T> or DataView).

Marc
 
M

Marc Gravell

I fired up the free but excellent "Lutz Roeder's .NET Reflector"
Rather embarrasingly, I've just noticed that you can also get this
from the Object Browser in Visual Studio... and as such is probably a
better option for occasional use.

Marc
 
N

NvrBst

Rather embarrasingly, I've just noticed that you can also get this
from the Object Browser in Visual Studio... and as such is probably a
better option for occasional use.

Marc

Thank you for the replys, they were helpful.

NB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top