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

Going nuts with DataBinding a ListBox (winforms)

I've setup the simplest of tests; A form with a ListBox and a Button. The
ListBox is bound to a List<string>. When you click the Button, a new string
is added to the list.

I can't for the life of me get the ListBox to update when new items are
added to it's DataSource!
<code>
List<stringdateTimeStringList = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
if(listBox1.DataSource == null)
{
listBox1.DataSource = dateTimeStringList;
}

dateTimeStringList.Add(DateTime.Now.ToString());
}
</code>

The only way I have been able to get it to work is to set the DataSource =
null, then set it back to the List<string>
Am I expecting it to do something it shouldn't? Currently it seems as
though all it does is populate the list - there is no persistent link with
the source data.

I must be doing this very wrong?
Thanks for any suggestions or help,
Steve
Mar 1 '07 #1
1 10925
sklett wrote:
I've setup the simplest of tests; A form with a ListBox and a Button.
The ListBox is bound to a List<string>. When you click the Button, a
new string is added to the list.

I can't for the life of me get the ListBox to update when new items
are added to it's DataSource! <code>
List<stringdateTimeStringList = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
if(listBox1.DataSource == null)
{
listBox1.DataSource = dateTimeStringList;
}

dateTimeStringList.Add(DateTime.Now.ToString());
}
</code>

The only way I have been able to get it to work is to set the
DataSource = null, then set it back to the List<stringAm I
expecting it to do something it shouldn't? Currently it seems as
though all it does is populate the list - there is no persistent link
with the source data.

I must be doing this very wrong?
Thanks for any suggestions or help,
List<Tdoesn't raise an event that something was changed inside it,
as it doesn't implement IBindingList. This thus means that the bound
control can't 'observe' the datasource, so it can't know if something
changed inside List<T>.

You should bind a BindingList, which you can create by using:
BindingList<stringtoBind = new
BindingList<string>(dateTimeStringList);
and then bind toBind to the listbox.

BindingList does implement IBindingList and will raise an event when
something changes, which means that the control (listbox) will know
that and will act accordingly.

FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Mar 2 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dtblankenship | last post by:
Hello everyone, I know this question has been asked many times in the forums, and after spending a few days reading, I am still confused as to the answer. I have a ListBox (lstBox),...
1
by: Stephen | last post by:
I have a form with a ListBox and would like to set the DataSource property at design-time but I can't seem to declare any kind of variable that will show up in the list. I have tried a traditional...
4
by: pmcguire | last post by:
Can someone explain to me the difference between these two bindings? dim b as Binding b=New Binding(dsMyDataset.tblMyTable, "colMyColumn") b=New Binding(dsMyDataset,...
9
by: Dennis | last post by:
I have tried using Databinding for my application but always seem to find it very restrictive (maybe I don't completely understand it enough). I always seem to find it much easier to display a...
3
by: Alec MacLean | last post by:
Hi everyone, I have a ComboBox that when changed, calls a method to change the content of a ListBox. I'm also using the Listbox's SelectedIndexChanged event to change other control values on...
0
by: Wayne Sepega | last post by:
I have the following Object DataSource <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetCustomer" TypeName="Customers" DataObjectTypeName="Customer"...
1
by: Dave A | last post by:
Hi, I am struggling with two way databinding in WinForms and the DataGridView. I am binding to business object classes (rather than datatables). If I have a collection of these business...
1
by: Hoffmania | last post by:
Is there a good tutorial of getting databinding working with NHibernate collections such as bags with lazy loading. I can access the collection programaticly, but not directly with databinding.
3
by: itisthiyagu | last post by:
Hi, Im looking for ListBox which works like Windows-XP's Add/Remove programs listbox. where can i get this kind of listbox ? I want to show a text in ListItem & it should expand with its...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.