The WinForms Walkthrough shows how to bind a DevForce entity model using WinForms databinding.
You want to learn how to bind a collection of entities to a TextBox, ComboBox, and DataGrid.
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"); |