Presentation is loading. Please wait.

Presentation is loading. Please wait.

DEV397 Windows Forms Databinding Mike Henderlight Program Manager Windows Forms Microsoft Corporation.

Similar presentations


Presentation on theme: "DEV397 Windows Forms Databinding Mike Henderlight Program Manager Windows Forms Microsoft Corporation."— Presentation transcript:

1 DEV397 Windows Forms Databinding Mike Henderlight Program Manager Windows Forms Microsoft Corporation

2 Agenda Introduction Basics Writing a data source

3 Databinding Goals Enable databinding to anything: Lists Indexed Collections (IList) DataSets Data Items Business objects *Any* object Make Control Development easier /more consistent

4 Assumptions “Three tier” is the driving scenario Disconnected data Finite numbers of records Datasources all share Indexable collections Consistent programming model Each List is homogeneous Examples DataSet Web Service returning an array of objects

5 Basics...

6 What is databinding? Automatic display & update of data Reduces the amount of “boilerplate” code you have to write Change notification Automatically updating data source from controls Automatically updating controls from datasource Managing the "current item"

7 Types of databinding “Simple databinding” Property to property Control has no knowledge of binding Standard across all controls Example: TextBox1.Text to Customer.Name “Complex databinding” Binding to the List Control has knowledge of binding Control dependent Example: DataGrid1.DataSource = Customers

8 Types of Datasource Array No change notification so: No Add/Delete Limited update Assumes "binding type" is array type IList (ArrayList, CollectionBase) No change notification so: No Add/Delete Limited update Assumes "binding type" is: indexed accessor type or first item in the list type Dim customers() As Customer public Customer this[int index] { get; } Default Public ReadOnly Property Item(index As Integer) As Customer

9 Types of Datasource IBindingList Change Notification so: Add Delete Update Sorting Searching Example: DataView

10 Binding Managers CurrencyManager Manages binding for a list Manages the "current" item Raises CurrentChanged and PositionChanged events Controls get “bindable list” from CurrencyManager Point of "consumer access" PropertyManager Similar to CurrencyManager but for single object

11 Binding Managers BindingContext Manages CurrencyManagers and PropertyManagers Typically for a Form For all contained controls Creates CurrencyManagers and PropertyManagers On demand based on a DataSource and a DataMember Controls get CurrencyManager from BindingContext Manages hierarchical relationships Based on DataMember Enables Master-Details

12 4 Set 2 Set BindingContext CurrencyManager Binding How it works... DataSource Customers[0].Name = “Bill” 1 TextChanged 2 Validated 3Set 2 Changed 1 Set 3 CurrentChanged1

13 What do you care about? Datasource: Property/ListChanged event Causes control value to update Control: PropertyChanged event Causes Binding to be marked as dirty Control: Validated event Causes new value to be pushed to data source Can stop this using CausesValidation property BindingContext: Get CurrencyManager for DataSource/DataMember CurrencyManager: Position: Changes current item Begin/End/CancelCurrent: Controls update of datasource List: Get the data source Current: Get the "current item" CurrentChanged/Position Changed events

14 Binding Basics Using the CurrencyManager demo demo

15 Tips and Tricks BindingContext: DataSource/DataMember is a key Different DS/DM means different CurrencyManager Validation: Driven by Leave event Does happen for Menus, Toolbar Can force using Form.Validate EndCurrentEdit: Required to flush datasource Me.BindingManager(dataSet1, “Customers”) DOES NOT EQUAL Me.BindingManager(dataSet1.Customers)

16 Master-Details (DataSet) Dataset: Bind thro’ the relationships: BindingContext creates a RelatedCurrencyManager DataGrid1.DataSource = DataSet1 DataGrid1.DataMember = “Customers” DataGrid2.DataSource = DataSet1 DataGrid2.DataMember = “Customers.CustOrders” DataGrid1.DataSource = DataSet1 DataGrid1.DataMember = _ “Customers.CustOrders.OrderOrderItems” “Customers.CustOrders.OrderOrderItems” Customers Orders OrderItems CustOrders OrderOrderItems

17 Master-Details (Objects) List: Bind thro’ the property names: BindingContext creates a RelatedCurrencyManager DataGrid1.DataSource = MyCustomersList DataGrid1.DataMember = “Customers” DataGrid2.DataSource = MyCustomersList DataGrid2.DataMember = “Customers.Orders” DataGrid1.DataSource = MyCustomersList DataGrid1.DataMember = “Customers.Orders.OrderItems” Customers Orders (OrdersCollection) OrderItems (OrderItemsCollection)

18 Master-Details demo demo

19 Lookup table You can use a ComboBox or ListBox as a “lookup” table Example: State field in Customer object Bind ComboBox to States collection Set DisplayValue to “value” property name Bind SelectedValue to Customer.State ComboBox1.DataSource = MyStates ComboBox1.DisplayMember = “LongName” ComboBox1.ValueMember = “ShortName” ComboBox1.DataBindings.Add _ (“SelectedValue”, DataSet1, “Customers.State”) (“SelectedValue”, DataSet1, “Customers.State”)

20 Lookup Table demo demo

21 Formatting Example: Format 12 as $12.00 Use the Format and Parse events on the Binding: Format and Parse are “paired” You need to do both Use BCL Formatting to actually format the value AddHandler TextBox1.DataBindings("Text").Format, _ AddressOf Me.TextBox1_Format AddressOf Me.TextBox1_FormattextBox1.DataBindings["Text"].Format +=new ConvertEventHandler(Form1_Format); +=new ConvertEventHandler(Form1_Format);

22 Formatting demo demo

23 Displaying errors You can set errors on the DataSet: Use ErrorProvider to display this information DataGrid automatically displays this information Use IDataErrorInfo on your business objects TIP: Don’t throw exceptions. We eat them! row.RowError = "You need to update the Widget price" row.SetColumnError("WidgetPrice", “Bad Widget Price")

24 Displaying Errors demo demo

25 Tips and Tricks (2) Datasets: You always bind to a DataView but not to the DefaultView The DataSet generates them on demand You can get to the DataView thro’ the CurrencyManager Use a DataViewManager to apply default Sorts and Filters Esp. useful for “child views” in a Master- Details Form

26 Using a DataViewManager demo demo

27 Writing a Data Source...

28 Datasource Interfaces Data Lists IList – indexed list IBindingList – list that supports change events searchable sortable IListSource – object that returns a list ITypedList – list with schema Data Items IEditableObject – Commit/Cancel IDataErrorInfo – error information ICustomTypeDescriptor – custom type information

29 IBindingList We will focus on change notification, Add, Remove, Update: public interface IBindingList : IList { // Properties bool AllowEdit { get; } bool AllowNew { get; } bool AllowRemove { get; }... bool SupportsChangeNotification { get; }... // Events public event ListChangedEventHandler ListChanged; // Methods... bool SupportsChangeNotification { get; }... // Events public event ListChangedEventHandler ListChanged; // Methods... object AddNew(); object AddNew();... }

30 Writing a custom data source demo demo

31 Community Resources http://www.microsoft.com/communities/default.mspx Most Valuable Professional (MVP) http://www.mvp.support.microsoft.com/ Newsgroups Converse online with Microsoft Newsgroups, including Worldwide http://www.microsoft.com/communities/newsgroups/default.mspx User Groups Meet and learn with your peers http://www.microsoft.com/communities/usergroups/default.mspx

32 evaluations evaluations

33 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.


Download ppt "DEV397 Windows Forms Databinding Mike Henderlight Program Manager Windows Forms Microsoft Corporation."

Similar presentations


Ads by Google