473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Binding an ArrayList to ListBox with Sort property set

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
4 5867
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Denzil | last post by:
Hi, I am using the DataView.Sort Property (on a DataView column) to sort the View based on some condition. Now the column that I need to sort contains characters and HTML code that should not...
0
by: berg | last post by:
All, I am binding an ArrayList full of custom objects to an DataGrid. The property in the custom class are all public. I define a DataGridTableStyle and set the MappingName to "ArrayList". I...
2
by: steve smith | last post by:
Hi I am new to this language, and still don't understand most of the concepts, I am trying to sort an arraylist by alphabet, I belive i need to use the IComparer interface, but have no idea how...
0
by: Jason Freeman | last post by:
Hi, (Cross-posted to microsoft.public.dotnet.framework.adonet) I've got a form with a listbox and a textbox on it. Both are bound to a DataView, which is sorted on the "Name" field of the...
0
by: TM | last post by:
I was able to find the code to list the name and email from the contacts in Outlook 2003, but the sort property is not working. Here is my sample code. For now I am just adding an item to a...
11
by: Just Me | last post by:
This doesn't work. I guess New is needed someplace but I don't know where? Public Property Filters() As ArrayList Get Filters.Add(cboFilter.Text)
4
by: Jason | last post by:
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...
1
by: JNariss | last post by:
Hello, I have created a form called frmS2P with the following: 1 listbox called List11 which holds the contents of a query created off my table called tblRequestActions. The fields which the...
2
by: Randy | last post by:
I have two listboxes on a form. The first box displays categories while the second box displays the items belonging to the category selected in the first box. Thus, the second box is essentially...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.