Going nuts with DataBinding a ListBox (winforms)

S

sklett

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<string> dateTimeStringList = 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
 
F

Frans Bouma [C# MVP]

sklett said:
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<string> dateTimeStringList = 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,

List<T> doesn'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<string> toBind = 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#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top