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

Code sample: WinForms walkthrough (WinForms)

Last modified on September 21, 2015 19:45

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; }
  }
VB
Public ReadOnly Property Self() As Category
   Get
       Return Me
   End Get
End Property

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

Notes

The download zip file also includes a sample written in VB showing how to accomplish the data binding using the WinForms Databinders.


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