By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Binding an ArrayList to ListBox with Sort property set

P: n/a
Here is an odd issue. I am trying to shed some light on why this is causing
a problem.

I have an ArrayList. I am binding it to a ListBox control with has its Sort
property set to True. If the ArrayList only has one element in it everything
works ok. But as soon as I have more than one element, I get the following
exception when the control loads up: "Cannot modify the Items collection
when the DataSource property is set.".

Anybody know what is happening?

- Jason
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Jason,

Like it says, if you want to modify (sort) the items, then you need to add them manully. However, you could sort the ArrayList before binding it to the ListBox.

On Fri, 06 May 2005 22:06:01 +0200, Jason <Ja*******@hotmail.com> wrote:
Here is an odd issue. I am trying to shed some light on why this is causing
a problem.

I have an ArrayList. I am binding it to a ListBox control with has its Sort
property set to True. If the ArrayList only has one element in it everything
works ok. But as soon as I have more than one element, I get the following
exception when the control loads up: "Cannot modify the Items collection
when the DataSource property is set.".

Anybody know what is happening?

- Jason


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #2

P: n/a
So the Sort property on the ListBox actually modifies the underlying
DataSource instead of just how it is viewed in the ListBox?

- Jason

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sqdgy5o1klbvpo@stone...
Hi Jason,

Like it says, if you want to modify (sort) the items, then you need to add
them manully. However, you could sort the ArrayList before binding it to
the ListBox.

On Fri, 06 May 2005 22:06:01 +0200, Jason <Ja*******@hotmail.com> wrote:
Here is an odd issue. I am trying to shed some light on why this is
causing
a problem.

I have an ArrayList. I am binding it to a ListBox control with has its
Sort
property set to True. If the ArrayList only has one element in it
everything
works ok. But as soon as I have more than one element, I get the
following
exception when the control loads up: "Cannot modify the Items collection
when the DataSource property is set.".

Anybody know what is happening?

- Jason


--
Happy coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #3

P: n/a
Not really. If a ListBox has a DataSource it makes no attempt to sort and setting Sorted = true will therefore cause an exception. ListBox blindly displays whatever is inside the DataSource.

If there is no datasource, that means it has full control of the list and can sort as much as it wants.

It may have something to do with the fact that various IList sources can have different ways of sorting.
On Mon, 09 May 2005 15:52:46 +0200, Jason <Ja*******@hotmail.com> wrote:
So the Sort property on the ListBox actually modifies the underlying
DataSource instead of just how it is viewed in the ListBox?

- Jason

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sqdgy5o1klbvpo@stone...
Hi Jason,

Like it says, if you want to modify (sort) the items, then you need to add
them manully. However, you could sort the ArrayList before binding it to
the ListBox.

On Fri, 06 May 2005 22:06:01 +0200, Jason <Ja*******@hotmail.com> wrote:
Here is an odd issue. I am trying to shed some light on why this is
causing
a problem.

I have an ArrayList. I am binding it to a ListBox control with has its
Sort
property set to True. If the ArrayList only has one element in it
everything
works ok. But as soon as I have more than one element, I get the
following
exception when the control loads up: "Cannot modify the Items collection
when the DataSource property is set.".

Anybody know what is happening?

- Jason


--
Happy coding!
Morten Wennevik [C# MVP]



--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #4

P: n/a
Thanks for the info Morten. It must be what you said... the way IList is
being implemented. I know this because I know I am not messing with the
items after being bound and when I upset the sorting property on the listbox
the exception goes away.

- Jason

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sqihnoxzklbvpo@stone...
Not really. If a ListBox has a DataSource it makes no attempt to sort and
setting Sorted = true will therefore cause an exception. ListBox blindly
displays whatever is inside the DataSource.

If there is no datasource, that means it has full control of the list and
can sort as much as it wants.

It may have something to do with the fact that various IList sources can
have different ways of sorting.
On Mon, 09 May 2005 15:52:46 +0200, Jason <Ja*******@hotmail.com> wrote:
So the Sort property on the ListBox actually modifies the underlying
DataSource instead of just how it is viewed in the ListBox?

- Jason

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sqdgy5o1klbvpo@stone...
Hi Jason,

Like it says, if you want to modify (sort) the items, then you need to
add
them manully. However, you could sort the ArrayList before binding it
to
the ListBox.

On Fri, 06 May 2005 22:06:01 +0200, Jason <Ja*******@hotmail.com> wrote:

Here is an odd issue. I am trying to shed some light on why this is
causing
a problem.

I have an ArrayList. I am binding it to a ListBox control with has its
Sort
property set to True. If the ArrayList only has one element in it
everything
works ok. But as soon as I have more than one element, I get the
following
exception when the control loads up: "Cannot modify the Items
collection
when the DataSource property is set.".

Anybody know what is happening?

- Jason


--
Happy coding!
Morten Wennevik [C# MVP]



--
Happy coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.