25
Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Rajiv Sodhi Microsoft India Microsoft India

Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Embed Size (px)

Citation preview

Page 1: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Making the Most of WinForms 2.0Data Binding

Making the Most of WinForms 2.0Data Binding Rajiv SodhiRajiv Sodhi

Microsoft IndiaMicrosoft India

Page 2: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Agenda

Introduction to Data BindingIntroduction to Data Binding

New Objects and EnhancementsNew Objects and Enhancements

Master-Detail Data BindingMaster-Detail Data Binding

Object Data BindingObject Data Binding

Page 3: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Data Sources Window Drag and drop operations

Dragging/dropping tables from Data Dragging/dropping tables from Data Sources Window onto form will create Sources Window onto form will create instances of:instances of:

DataSet, TableAdapter, BindingSourceDataSet, TableAdapter, BindingSource

BindingNavigator with nav/add/delete/save BindingNavigator with nav/add/delete/save controlscontrols

Bound control(s)Bound control(s)

Use Data Sources window to Use Data Sources window to configure:configure:

Whether table will be edited with DataGridView Whether table will be edited with DataGridView or “Details” mode (individual controls)or “Details” mode (individual controls)

Mapping of detail control types to column data Mapping of detail control types to column data types (shortcut to Windows Forms types (shortcut to Windows Forms Designer/Data UI Customization node in Designer/Data UI Customization node in Options dialog)Options dialog)

Page 4: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Data Source Options

Database (DataSet)Database (DataSet)Any ADO.NET data Any ADO.NET data sourcesource

Web ServiceWeb ServiceBind to parameterized Bind to parameterized methods of a Web methods of a Web service, for select, service, for select, update, delete, and update, delete, and insert operationsinsert operations

ObjectObjectBind to instances of Bind to instances of classes and their classes and their properties (more later!)properties (more later!)

Page 5: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Smart Defaults

Control namesControl names

Label captionsLabel captions

Controlling through Caption property Controlling through Caption property of Column objects in typed DataSetsof Column objects in typed DataSets

Automatic addition of typed DataSets Automatic addition of typed DataSets and their DataTables to toolboxand their DataTables to toolbox

Page 6: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Data Sources Window - Drag and DropCompletely CustomizableChanging existing bindingChanging existing binding

Establishing list/lookup bindingEstablishing list/lookup bindingDisplayMemberDisplayMember

ValueMemberValueMember

SelectedValueSelectedValue

Use of SmartTagUse of SmartTag

More Smart DefaultsMore Smart Defaults

Page 7: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Drag and Drop Binding

Data Sources WindowData Sources Window

Drag and Drop Data BindingDrag and Drop Data Binding

Binding ControlsBinding Controls

Page 8: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Agenda

Introduction to Data BindingIntroduction to Data Binding

New Objects and EnhancementsNew Objects and Enhancements

Master-Detail Data BindingMaster-Detail Data Binding

Object Data BindingObject Data Binding

Page 9: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

WinForms 2.0 Data BindingIntroduction to ObjectsTyped DataSetTyped DataSet

DataTable, TableAdapterDataTable, TableAdapter

BindingSourceBindingSource

[BindingNavigator][BindingNavigator]

Page 10: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Strongly-Typed DataSets

As beforeAs beforeConsist of DataTables, Relations, ConstraintsConsist of DataTables, Relations, Constraints

Drag/drop from Server Explorer is supportedDrag/drop from Server Explorer is supported

Based on XML schema and code-behind classBased on XML schema and code-behind class

Also consists of TableAdaptersAlso consists of TableAdaptersEncapsulate a DataAdapter and a ConnectionEncapsulate a DataAdapter and a Connection

Enhanced Typed Dataset DesignerEnhanced Typed Dataset DesignerInstead of XSD Designer earlierInstead of XSD Designer earlier

Partial class supportPartial class support

Page 11: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

TableAdapters

Have Queries (methods for handling Have Queries (methods for handling selects, updates, inserts, deletes)selects, updates, inserts, deletes)

Queries exists as methods Queries exists as methods

Fully customizable and extendableFully customizable and extendable

Can be re-used across multiple formsCan be re-used across multiple forms

Have Connection propertyHave Connection propertyConnection string typically stored in Connection string typically stored in web/app.config; default value applied in web/app.config; default value applied in WinForms apps.WinForms apps.

Page 12: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

BindingSource: Basics

Acts as “broker” between bound Acts as “broker” between bound controls and actual data sourcecontrols and actual data source

DataSource, DataMemberDataSource, DataMemberPoint to typed DataSet, DataTablePoint to typed DataSet, DataTable

AllowEdit, AllowNew, AllowRemove AllowEdit, AllowNew, AllowRemove

Filter, Sort propertiesFilter, Sort propertiesRun time: RemoveFilter, RemoveSort Run time: RemoveFilter, RemoveSort methodsmethods

Page 13: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

BindingSourceRun time members

MovePrevious, MoveNext, MoveFirst, MovePrevious, MoveNext, MoveFirst, MoveLast, Position, Count properties MoveLast, Position, Count properties (handles all navigation)(handles all navigation)

AddNew, Insert(index, obj) methodsAddNew, Insert(index, obj) methods

RemoveCurrent , RemoveAt(index), RemoveCurrent , RemoveAt(index), Remove(obj)Remove(obj)

EndEdit, CancelEdit methodsEndEdit, CancelEdit methodsWorks with edits Works with edits andand inserts/new’s inserts/new’s

Use of most of these is optional due Use of most of these is optional due to…to…

Page 14: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

BindingNavigator

UI provider that wraps navigation and UI provider that wraps navigation and maintenance functionality of maintenance functionality of BindingSource BindingSource NotNot like old VB data control like old VB data control

Has UI componentHas UI componentSpecialized toolstripSpecialized toolstrip

Has tray component tooHas tray component tooSelecting one will select bothSelecting one will select both

Members that associate ToolStrip Members that associate ToolStrip controls with specific actorscontrols with specific actors

DeleteItem = bindingNavigatorDeleteItem DeleteItem = bindingNavigatorDeleteItem (ToolStripButton)(ToolStripButton)

Page 15: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

How It All Connects

Bin

din

gS

ou

rce

Bin

din

gS

ou

rce

Dat

aSet

Dat

aSet

Dat

aTab

leD

ataT

able

DataSourceDataSource

DataMemberDataMember

SimpleSimpleControlControl

ComplexComplexControlControl

Co

lum

nC

olu

mnDataBindingDataBinding

PropertyProperty

Changes Changes control control

contents via contents via navigation, navigation,

maintenancemaintenanceBin

din

gN

avig

ato

rB

ind

ing

Nav

igat

or

BindingSourceBindingSource

DataSourceDataSource

Page 16: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Complex and Simple Binding Scenarios

Complex BindingComplex BindingDataGridView.DataSource = BindingSourceDataGridView.DataSource = BindingSource

SimpleBindingSimpleBindingTextBox.Text (and other controls/string-based TextBox.Text (and other controls/string-based properties) bound to member of BindingSourceproperties) bound to member of BindingSource

BindingNavigator.BindingSource = BindingNavigator.BindingSource = BindingSourceBindingSource

Page 17: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Introducing New Objects

Typed Dataset DesignerTyped Dataset Designer

Customizable Typed TableAdapterCustomizable Typed TableAdapter

Complex Binding ScenarioComplex Binding Scenario

Simple Binding ScenarioSimple Binding Scenario

Page 18: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Agenda

Introduction to Data BindingIntroduction to Data Binding

New Objects and EnhancementsNew Objects and Enhancements

Master-Detail Data BindingMaster-Detail Data Binding

Object Data BindingObject Data Binding

Page 19: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Master-Detail Scenarios

Binding to child rowsBinding to child rows

Use of Typed DataSet Use of Typed DataSet Relations/Foreign Keys as data Relations/Foreign Keys as data sourcessources

Precision drag & drop techniquesPrecision drag & drop techniquesDrop the child table, not the siblingDrop the child table, not the sibling

Page 20: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Master-Detail Binding

Page 21: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Agenda

Introduction to Data BindingIntroduction to Data Binding

New Objects and EnhancementsNew Objects and Enhancements

Master-Detail Data BindingMaster-Detail Data Binding

Object Data BindingObject Data Binding

Page 22: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Object Binding

Binding to single objectsBinding to single objects

Binding to BindingList<t>Binding to BindingList<t>

AllowNew scenarios: AddingNew AllowNew scenarios: AddingNew events on BindingSource and events on BindingSource and BindingList<t>BindingList<t>

Support for master-detail bindingSupport for master-detail binding

Page 23: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Object Binding

Page 24: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

Resources

MSDN Smart Client Developer Center:http://msdn.microsoft.com/smartclient/Soup to Nuts: Build Windows Forms Applications with .NET(Webcasts and Virtual Labs):http://www.microsoft.com/events/series/windowsforms.mspx

WindowForms.NET Whidbey Page:http://windowsforms.net/Default.aspx?tabindex=5&tabid=60Improved Data Support Page:http://windowsforms.net/WhidbeyFeatures/default.aspx?PageID=2&ItemID=8&Cat=Runtime&tabindex=5

Newgroups:microsoft.public.dotnet.framework.windowsformscomp.lang.basic.visual.misc

Page 25: Making the Most of WinForms 2.0 Data Binding Rajiv Sodhi Microsoft India

© 2005 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.