CollectionBase & DataGrid --- Please help!!

  • Thread starter Thread starter Steve M
  • Start date Start date
S

Steve M

I have subclassed CollectionBase. I have also implemented GetEnumerator().
I have tried to set the DataSource of a DataGrid to an instance of my
subclass. However, the items in the grid are not obtained via the Enumerator
that I create in the GetEnumerator() method.
I have tried elplicitly implementing IEnumerable on my sublass as well.
Can anyone help on this? Thanks in advance.
 
Steve,
CollectionBase already implements IEnumerator & the GetEnumerator method.

What you are really doing, that the base version is not working, Can you
post some code?

Hope this helps
Jay
 
CollectionBase already implements IEnumerator & the GetEnumerator method.
What you are really doing, that the base version is not working, Can you
post some code?

Jay,

CollectionBase implements IEnumerable.

In my subclass I have created my own new GetEnumerator() method (oddly
enough, MS decided not to make that method virtual).

If I set the DataSource of a DataGrid to be an instance of my new subclass,
it does not work properly.

I can, however, get it to work properly in a DataGrid if I explicitly
implement IList in my subclass -- even though CollectionBase already
implements it. When I do this, the DataGrid properly fills all my items.
(Although I still never see my GetEnumerator() method called--but at least
the DataGrid now works.)

Thanks.
 
Steve,
CollectionBase implements IEnumerable.
Doh! I knew that I typed the wrong one :-(
In my subclass I have created my own new GetEnumerator() method (oddly
enough, MS decided not to make that method virtual).
Why have you created a new one???

CollectionBase encapsulates an ArrayList, CollectionBase.GetEnumerator
returns this ArrayList's Enumerator.

Classes that derive from CollectionBase operate on this ArrayList via the
protected List or InnerList properties. CollectionBase.List causes the
protected virtual CollectionBase.On* methods to be called, while
CollectionBase.InnerList allows direct access to the ArrayList itself,
without calling the CollectionBase.On* methods.

Not being virtual seems to be the correct design decision for the class!

So I have to ask:

Why are you creating your own GetEnumerator method?
Can you post code on what you are really doing?
I can, however, get it to work properly in a DataGrid if I explicitly
implement IList in my subclass -- even though CollectionBase already
implements it. When I do this, the DataGrid properly fills all my items.
(Although I still never see my GetEnumerator() method called--but at least
the DataGrid now works.)
Correct! CollectionBase implements IList and directs every thing to its
encapsulated ArrayList. However you may have created a new GetEnumerator in
your class, CollectionBase, hence its IList implementation will not know
about it.

So again:
Why are you creating your own GetEnumerator method?
Can you post code on what you are really doing?

Hope this helps
Jay
 
Well, I don't want to post my code.
I got it working -- though since I can't see the source code for DataGrid, I
don't know why it behaves the way it does.
I created my own GetEnumerator() because my CollectionBase subclass requires
its own special enumerator -- it's just the way the clas works.
 
Steve,
Well, I don't want to post my code.
That's understandable.
I created my own GetEnumerator() because my CollectionBase subclass requires
its own special enumerator -- it's just the way the clas works.
If you need your own enumerator, then I would suggest you not use
CollectionBase as CollectionBase was not really designed to replace its
enumerator... I suspect you had to replace enough of CollectionBase, that it
will be cleaner to start a collection from scratch (a Class that implements
IList itself).

Hope this helps
Jay

Steve M said:
Well, I don't want to post my code.
I got it working -- though since I can't see the source code for DataGrid, I
don't know why it behaves the way it does.
I created my own GetEnumerator() because my CollectionBase subclass requires
its own special enumerator -- it's just the way the clas works.
<<snip>>
 
Back
Top