473,664 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<stringdate TimeStringList = new List<string>();
private void button1_Click(o bject sender, EventArgs e)
{
if(listBox1.Dat aSource == null)
{
listBox1.DataSo urce = dateTimeStringL ist;
}

dateTimeStringL ist.Add(DateTim e.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 10940
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<stringdate TimeStringList = new List<string>();
private void button1_Click(o bject sender, EventArgs e)
{
if(listBox1.Dat aSource == null)
{
listBox1.DataSo urce = dateTimeStringL ist;
}

dateTimeStringL ist.Add(DateTim e.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<str ingtoBind = new
BindingList<str ing>(dateTimeSt ringList);
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
4203
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), SqlConnection (sqlConnection), SqlDataAdapter (daLookupData), SqlDataAdapter (daData), DataSet (dsLookupData), and DataSet (dsData), all created via the IDE during design-time. Here is the design of my simple tables: (An example since I can't post my...
1
2565
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 array, ArrayList, Collection as public vars in the Form Class's Declarations section. Setting the DataSource property to any of these works fine at run time. I want to do it at design time because after I change the Collection, it doesn't...
4
2192
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, "tblMyTable.colMyColumn") They produce different behavior in my application. I know which one I must use, but I don't understand why.
9
2095
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 form, have the user fill it out then put the data into a class representing a data row and then use the OLEadaptor or OLECommands to update the database. My question is has anyone really used databinding in anything but the simpliest application...
3
7754
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 the form. (It's a Company -> People -> Personal Data relationship) When I bind my datasource to the ListBox, I find that the
0
1322
by: Wayne Sepega | last post by:
I have the following Object DataSource <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetCustomer" TypeName="Customers" DataObjectTypeName="Customer" InsertMethod="InsertCustomer" OnUpdated="ObjectDataSource1_Updated" UpdateMethod="UpdateCustomer" OnSelected="ObjectDataSource1_Selected">
1
18835
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 objects that is the datasource of a DataBinding that is bound to a DataGridView (WinForms) then I was expecting that any change to the data would be reflected in the DataGridView (in other words I was expecting the 2 way databinding to actually...
1
2258
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
1574
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 description when user selects a particular item. Also i want to add a button into to ListItem. it is same like Add/Remove programs ListBox. anybody help me. -Thiyagu
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7375
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.