473,322 Members | 1,403 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,322 software developers and data experts.

Modify ListBox Items while using DataSource in C#

I tried to fill a ListBox with a DataSource pointing to an ArrayList.

It all works fine...up to one point.

The ArrayList is dynamic, which means the contents of it change, during the course of program use.
But instead of Updating the ListBox, virtually nothing happens. The items that were written into the ListBox during the first run remain inside the ListBox, assuming the new values of the ArrayList. Obviously, if the new ArrayList is shorter than the original List, a click on an basically obsolete makes the program crash, since it is an unreferenced object.

This code is used to change the items:

Expand|Select|Wrap|Line Numbers
  1. AL.Clear(); //Empties the ArrayList, fresh start
  2. while(DataAvailable) //Reads data from an external source
  3. AL.Add(new ALList(name, content)); // Adds a custom class to the ArrayList, consisting of a name and a content.
  4. ListBox.DataSource = AL; //Sets the DataSource to ArrayList.
  5. ListBox.DisplayMember = "Display"; //Chooses the name from the custom class
  6. ListBox.ValueMember = "Content"; //Sets the Value to the Content.
I have already tried these measures:
Expand|Select|Wrap|Line Numbers
  1. ListBox.DataSource = null; //Frees the DataSource, Items become accessible again
  2. ListBox.Items.Clear(); //Empties the ListBox by Force.
  3. ListBox.DataSource = AL; //Assigns the DataSource AL to the LB again.
But to no avail. The old contents of the ListBox remain shown and Clickable.

I've run out of ideas and would like to ask for your expertise now.
Dec 14 '07 #1
1 8279
I'm hereby closing this post...I've come across the solution myself.

Here's what I did:

I moved the ArrayList from a global (available to all functions) to a local (available to only the calling function) position.
The result: Each time the function is called, a new ArrayList is created and the old one is getting destroyed.

As a result, when the DataSource of the ListBox is assigned, it gets assigned a new ArrayList...and then correctly displays the items within the ArrayList.

To make long things short:
As long as the DataSource changes instead of only being given new values, it will work.

Thank you for reading and I hope my solution helps someone else.
Dec 15 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Luca | last post by:
Hello, I'm using a windows form in which there is a standard ListBox control. I want to add/remove objects from the ArrayList associated to the ListBox, and I want the ListBox immediately shows...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
1
by: yamne | last post by:
I have a problem. When I click in edit datagrid button I show two listbox and two button. I use two button to move data between two listbox. My problem is that I can't call the listbox in the...
3
by: Joey | last post by:
Hi, I'm trying to add a default item to my listbox but when I do it tells me that it's not defined, could someone tell me the syntax I need to use to get the listbox control to display a default...
1
by: VitorJOL | last post by:
Hello I have a listbox with a datasource.I want to remove a row from listbox but not remove it from the database. How can i do this ? PROBLEM:"Cannot modify the Items collection when the...
3
by: joe | last post by:
I actually have 2 questions: 1) Is databinding the fastest way to load a listbox from sqlserver? speed is crucial and I want to make sure i'm populating it the fastest way i can 2) Also,...
12
by: Art | last post by:
Hi, I've got a number of ListBoxes that hold data from an Access database. On another tab of my form I need the user to use a ComboBox to choose one of the items from one of the ListBoxes. I...
3
by: Will Chamberlain | last post by:
I am running into a strange error when I try debugging my current application. A form loads up with a dynamically-populated textbox full of software titles. Upon selection of a software title and...
3
by: perrycheung221 | last post by:
Hi guys, I got 2 listboxes in a window form, one on left and one on right. The 1st listbox have some items while the 2nd listbox is empty. Also there are 2 buttons between the 2 listboxes which...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.