Data Binding 101. How to do it in .Net

This seems like an obvious achievement, but I just can't figure it out. Say I have a list of strings. How to associate it with a list to update the list as data from the list? I am using vb.net.

I have tried this so far. I managed to display the data but not change it:

Public Class Form1 Private mycountries As New List(Of String) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.Sort() ListBox1.DataSource = mycountries 'this works fine End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click mycountries.RemoveAt(0) ListBox1.DataSource = mycountries 'this does not update End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click MsgBox(mycountries(0)) End Sub End Class 

I also tried this:

 ListBox1.DataBindings.Add("items", mycountries, "Item") 

But the items are read-only, so they do not work.

Also, if I want to associate the included property of a button with a boolean, how can I do this? I tried this, but I do not know what to add for the last parameter.

 Dim b As Boolean = True Button3.DataBindings.Add("Enabled", b, "") 
+4
source share
2 answers

You need to use a collection that supports change notifications for your data source. When you remove an item from a simple list, it does not tell anyone about it.

The following is an example of using a BindingList :

 Public Class Form1 Private mycountries As New BindingList(Of String) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") ' BindingList doesn't have a Sort() method, but you can sort your data ahead of time ListBox1.DataSource = mycountries 'this works fine End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click mycountries.RemoveAt(0) ' no need to set the DataSource again here End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click MsgBox(mycountries(0)) End Sub End Class 

For your second question, you are not bound to a variable when adding a data binding. You are attached to an object (which acts as a data source), and then you specify a property on that object that will give you the value to which you want to bind.

So, for the button, you need something like this (apologies for C #, but you get this idea):

 public class SomeModel { public bool ButtonEnabled { get; set; } } public class Form1 : Form { public Form1() { InitializeComponent(); SomeModel model = new SomeModel(); // first parameter - button property that should be bound // second parameter - object acting as the data source // third parameter - property on the data source object to provide value button1.DataBindings.Add("Enabled", model, "ButtonEnabled"); } 

In general, data binding refers to a change notification. If you need to bind to user objects, look into the INotifyPropertyChanged interface.

+6
source

You can do this trick:

 ListBox1.DataSource = Nothing mycountries.RemoveAt(0) ListBox1.DataSource = mycountries 
0
source

All Articles