Up Model code samples
DevForce 2010 Resource Center » Code samples » Model code samples » Code sample: WinForms walkthrough (WinForms)

Code sample: WinForms walkthrough (WinForms)

Last modified on March 13, 2012 12:34

The WinForms Walkthrough shows how to bind a DevForce entity model using WinForms databinding.


Problem

You want to learn how to bind a collection of entities to a TextBox, ComboBox, and DataGrid.

Solution

This WinForms solution shows a master-detail view. The master view on the left is implemented with a DataGrid and allows you to select a Product of interest. The detail view on the right allows you to edit the properties of the selected Product. The master and detail views are synchronized because they are using the same datasource (and therefore share the same .NET CurrencyManager).

The ComboBox databinding is performed in an object-oriented fashion, by binding the Supplier property of the Product (a navigation property that returns the Supplier Entity for a Product), to the Self property on the Entity which was defined on the partial class as:

C#
 public Supplier Self {
   get { return this; }
  }

The WinForms databinding code to bind to these controls looks like this:

C#
  productNameTextBox.DataBindings.Add("Text", Products, "ProductName", true,
                                      DataSourceUpdateMode.OnPropertyChanged);
  productQuantityPerUnitTextBox.DataBindings.Add("Text", Products, "QuantityPerUnit", true,
                                                 DataSourceUpdateMode.OnPropertyChanged);
  productPriceTextBox.DataBindings.Add("Text", Products, "UnitPrice", true,
                                       DataSourceUpdateMode.OnPropertyChanged);
  productUnitsInStockTextBox.DataBindings.Add("Text", Products, "UnitsInStock", true,
                                              DataSourceUpdateMode.OnPropertyChanged);
  productUnitsOnOrderTextBox.DataBindings.Add("Text", Products, "UnitsOnOrder", true,
                                              DataSourceUpdateMode.OnPropertyChanged);

  productPriceTextBox.DataBindings[0].FormatString = "c";

  categoryComboBox.DataSource = Categories;
  categoryComboBox.DisplayMember = Category.PathFor(c => c.CategoryName);
  categoryComboBox.ValueMember = Category.PathFor(c => c.Self);
 // Bind ComboBox.SelectedValue to the Product.Category entity
 categoryComboBox.DataBindings.Add("SelectedValue", Products, "Category");

  supplierComboBox.DataSource = Suppliers;
  supplierComboBox.DisplayMember = Supplier.PathFor(s => s.CompanyName);
  supplierComboBox.ValueMember = Supplier.PathFor(s => s.Self);
 // Bind ComboBox.SelectedValue to the Product.Supplier entity
 supplierComboBox.DataBindings.Add("SelectedValue", Products, "Supplier");

Prerequisites


Created by DevForce on December 05, 2011 14:28

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade