.NETGURU
Updating a simple ListBox from the DataSource
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.windowsforms.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Alex Maghen (VIP)
I have a simple ListBox "LB" and an ArrayList "TheList". I load the ArrayList
with a few items and then do

LB.DataSource = TheList;
LB.DisplayMember = "XXX";
LB.ValueMember = "YYY"; // or whatever.

Everything works fine and my values are displayed.

BUT, if I then add more items to my ArrayList, they are not reflected in the
ListBox. Is there a setting to have the ListBox do this automatically? If
not, how do I force it to re-load itself?

Thanks. Alex
Reply to this message...
 
    
Rami Saad (VIP)
Hello Alex,

In an ArrayList, the 'plumbing' is not available to support two-way binding
as with a dataset. So, you have to handle the synchronization yourself. One
way to do this is to set the listBox1.DataSource to null and then reset it
to your ArrayList. Another way is to use the CurrencyManager as shown in
the code below.

private System.Windows.Forms.ListBox listBox1;

private System.Windows.Forms.Button button1;

private ArrayList myArrayList;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

myArrayList = new ArrayList();

myArrayList.Add("orange");

myArrayList.Add("green");

myArrayList.Add("blue");

myArrayList.Add("red");

listBox1.DataSource = myArrayList;

}

......

//change the arraylist

private void button1_Click(object sender, System.EventArgs e)

{

myArrayList[1] = "pink";

myArrayList.Add("lavendar");

//use currency manger to sync up the listbox

BindingManagerBase bm =
this.listBox1.BindingContext[myArrayList];

CurrencyManager cm = (CurrencyManager) bm;

if (cm != null)

cm.Refresh();

//Or, you can just reset the datasource

//listBox1.DataSource = null;

//listBox1.DataSource = myArrayList;

}

Hope this helps.

Regards,
Rami Saad
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Reply to this message...
 
    
Alex Maghen (VIP)
Thanks for this. When I use the approach of setting the DataSource to NULL
and then back again, their's a strange delay of up to a second and a half or
more. Any idea why this might be? There are only three entries!

Thanks.

Alex

""Rami Saad"" wrote:

[Original message clipped]

Reply to this message...
 
 
System.Collections.ArrayList
System.EventArgs
System.Web.UI.WebControls.ListBox
System.Windows.Forms.BindingManagerBase
System.Windows.Forms.Button
System.Windows.Forms.CurrencyManager
System.Windows.Forms.ListBox




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification