473,385 Members | 1,325 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,385 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 5866
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.