Search:
Namespaces
Discussions
.NET v1.1
Feedback
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
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com