Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically bind a DataRepeater (Microsoft.VisualBasic.PowerPacks)
    text
    copied!<p>I am using a DataRepeater to show data from a business objects on the screen. I am using windows forms in C# to accomplish this. The datasource is not available at compile time so I want to bind the datasource at runtime.</p> <p>Here is the simplified scenario. I'm using this business class:</p> <pre><code>public class Product { private double _price; public double Price { get { return _price; } set { _price = value; } } } </code></pre> <p>I have created a ProductDataSource with the VisualStudio interface and bound the price to a label. Now I filled the datasource of my repeater in code:</p> <pre><code>dataRepeater1.DataSource = _productDataAgent.GetProducts(); </code></pre> <p>When I startup my application the prices are correctly filled in the labels. So far so good. </p> <p>Now I want the price labels to be updated when the product is updated. The Visual Studio interface helps me, and let me choose a 'Data Source Update Mode'. So I choose "OnPropertyChanged". </p> <p>Here comes the tricky part. How does the .NET runtime know that the price property is updated from the backend. So I modify my business class to implement INotifyPropertyChanged. Like this:</p> <pre><code>public class Product : INotifyPropertyChanged { private double _price; public double Price { get { return _price; } set { _price = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Price")); } } } public event PropertyChangedEventHandler PropertyChanged; } </code></pre> <p>The problem is this doesn't work. When I update a product it remeanes un-updated in the interface. When I debug and change the property, I see that the PropertyChanged event is null so no one is listening.</p> <p>Delving a little deeper in to the problem I found the following on the System.Windows.Forms.Binding Constructor page on MSDN:</p> <blockquote> <p>An event named PropertyNameChanged.</p> </blockquote> <p>So I tried using a (custom) PriceChanged event, but that did not work.</p> <p>Am I doing something wrong here? I am comming from using WPF, so maybe this works a little different in Windows Forms? Is this because I am binding at runtime?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload