Article ID: 313482 - Last Review: May 17, 2007 - Revision: 6.2 Roadmap for Windows Forms data bindingThis article was previously published under Q313482 On This PageSUMMARY This article provides a roadmap to learn and to master data
binding in Windows Forms applications. Roadmap articles provide links to useful
information, including online documentation, Microsoft Knowledge Base articles,
and Microsoft white papers, to help you learn about a Microsoft product or
technology. For additional information about ADO.NET objects, click the article number below to view the article in the Microsoft Knowledge Base: 313590
(http://support.microsoft.com/kb/313590/EN-US/
)
INFO: Roadmap for ADO.NET
OverviewThe Windows Forms object model provides a robust data binding infrastructure that you can use to write both data providers and data consumers. The Windows Forms object model supports both reflection-based data binding and interface-based data binding of data providers. Additionally, the Windows Forms object model supports a number of interfaces to extend existing control behavior and to add various binding behaviors to custom classes.Windows Forms can bind to the public properties of a class or to any class that supports the IList interface, such as the DataView, the Array, the ArrayList, and the Collection classes. Unlike ASP.NET Web Forms, the Windows Forms object model does not support binding to the DataReader object or the IEnumerable interface because backward scrolling support is required when you bind to anything other than a simple object. ArchitectureThe Windows Forms object model supports two different binding managers:
You can use the CurrencyManager class to bind simple or complex controls to an object that supports the IList interface, such as the DataView, the Array, the ArrayList, and the Collection objects. With a custom class that supports the IBindingList interface, you can sort the list or add and delete items in the list. The controls are bound to the public properties of the object that is returned by the list. This object can support the ICustomTypeDescriptor interface to bind to an indexed property instead of public properties. Additionally, this object can support the IEditableObject interface to provide change support that you can access through the BeginEdit, the EndEdit, and the CancelEdit methods. Unlike binding to an ADO Recordset object, the IList interface does not keep track of the "current" record. As a result, the IList interface does not include the MoveFirst, the MoveNext, the MovePrevious, and the MoveLast methods. Instead, the CurrencyManager maintains the currency through the Position property, and you can increment and decrement this property to move through records. If you want to be notified of user navigation, make sure that your application hooks the PositionChanged event of the CurrencyManager. Additionally, if you want to be notified of any user changes (such as additions, deletions, or sorting), make sure that your application hooks the ListChanged event of the IBindingList interface (which the DataView object implements). In general, do not use the user interface to change data programmatically. Instead, use the bound object (which is typically DataView) to change data. You can use the Microsoft Visual Studio .NET integrated development environment (IDE) to bind controls at design time to any class that supports the IComponent interface. You can also bind at run time. The act of binding takes two arguments: an object and a property. The following code binds a TextBox control and a DataGrid control to the same CurrencyManager and DataView: You can also traverse DataRelation objects in data binding syntax. In the following code, the second DataGrid uses the CustOrd DataRelation to display the Orders records for the current Customers record that is selected in the first DataGrid: For more information about Windows Forms data binding, see the following topics in the Microsoft Visual Studio .NET Help documentation:
Binding Class http://msdn2.microsoft.com/en-us/library/system.windows.forms.binding(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.binding(vs.71).aspx) BindingContext Class http://msdn2.microsoft.com/en-us/library/system.windows.forms.bindingcontext(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.bindingcontext(vs.71).aspx) QuickStart Samples, Walkthroughs, and Microsoft Knowledge Base ArticlesQuickStart sample files provide code samples for your reference. Walkthroughs provide mini-tutorials that walk you through typical application development scenarios. Microsoft Knowledge Base "How To" articles provide step-by-step instructions about how to accomplish specific tasks.The Visual Studio .NET Help documentation, QuickStart sample files, walkthroughs, and Microsoft Knowledge Base articles in the sections to follow describe how to use Windows Forms data binding. QuickStart Samples QuickStart sample files are installed on your computer in one of two locations. If you install the QuickStart sample files as part of Visual Studio .NET, the sample files are located in the following folder:
C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\QuickStart\...
If you install the QuickStart sample files as part of the
Microsoft .NET Framework, the sample files are located in the following folder:
C:\Program Files\FrameworkSDK\Samples\QuickStart\...
WalkthroughsTo access walkthroughs, click Index on the Help menu in Visual Studio .NET. In the Look For text box, type Walkthroughs, Windows Forms. A list of data access walkthroughs appears in the index results pane. Microsoft Knowledge Base Articles Click here to view a list of How To articles about Windows Forms data binding
(http://search.microsoft.com/results.aspx?q=Windows+Forms+data+binding&qsc0=2&FORM=QBME1&l=1&mkt=en-US&PageType=99&ft0=20000)
Binding to a DataView or a DataViewManagerThe class that is most commonly bound is the DataView class. Any binding to a DataSet, a DataTable, or a DataViewManager object derives from binding to a DataView class that the other classes provide. If you want to synchronize all of your controls to the same DataView, you must either bind them all to the same DataView object, or you must use compatible binding syntax so that the same DataView object is generated from the DataSource object.When you bind to a DataViewManager object, you can set filters on multiple DataTable objects in the DataSet simultaneously. When you navigate hierarchically, the DataViewManager generates a DataView object with the appropriate filter for each level in the hierarchy. Walkthroughs
Walkthrough: Simple Data Access in a Windows Form QuickStart Sampleshttp://msdn2.microsoft.com/en-us/library/aa984466(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa984466(VS.71).aspx) Walkthrough: Displaying Data in a Windows Form Using a Parameterized Query http://msdn2.microsoft.com/en-us/library/aa984467(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa984467(VS.71).aspx) Walkthrough: Creating a Master-Detail Windows Form http://msdn2.microsoft.com/en-us/library/aa984462(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa984462(VS.71).aspx) Data Binding with Windows Forms and .NET http://msdn2.microsoft.com/en-us/library/ms973824.aspx (http://msdn2.microsoft.com/en-us/library/ms973824.aspx)
Databinding in Windows Forms If you installed the QuickStart samples on your computer, the
following subfolders contain additional samples:
http://samples.gotdotnet.com/quickstart/winforms/doc/winformsdata.aspx (http://samples.gotdotnet.com/quickstart/winforms/doc/winformsdata.aspx)
Navigating Data in Windows Forms Microsoft Knowledge Base Articleshttp://msdn2.microsoft.com/en-us/library/aa983596(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa983596(VS.71).aspx) 317041
(http://support.microsoft.com/kb/317041/EN-US/
)
HOW TO: Retrieve DataView of a Windows Forms Bound Control in Visual Basic .NET
317164
(http://support.microsoft.com/kb/317164/EN-US/
)
HOW TO: Retrieve DataView of a Windows Forms Bound Control in Visual C# .NET
308070
(http://support.microsoft.com/kb/308070/EN-US/
)
HOW TO: Implement a Searchable DataGrid by Using ADO.NET and Windows Forms
317951
(http://support.microsoft.com/kb/317951/EN-US/
)
HOW TO: Hide a Column in a Windows Form DataGrid
308484
(http://support.microsoft.com/kb/308484/EN-US/
)
HOW TO: Display Parent and Child Records in a Windows Forms DataGrid by Using Visual C# .NET
308052
(http://support.microsoft.com/kb/308052/EN-US/
)
HOW TO: Display Parent and Child Records in a Windows Forms DataGrid by Using Visual Basic .NET
308454
(http://support.microsoft.com/kb/308454/EN-US/
)
HOW TO: Display Parent and Child Fields Together in a DataGrid by Using Visual C# .NET
308057
(http://support.microsoft.com/kb/308057/EN-US/
)
HOW TO: Display Parent and Child Fields Together in a Windows Forms DataGrid by Using Visual Basic .NET
307710
(http://support.microsoft.com/kb/307710/EN-US/
)
HOW TO: Custom Page a DataGrid Windows Control by Using Visual C# .NET
305271
(http://support.microsoft.com/kb/305271/EN-US/
)
HOW TO: Custom Page a DataGrid Windows Control by Using Visual Basic .NET
Binding to an Object or a List of ObjectsWhen you bind to a simple class (that is, a class that does not support the IList or the ICustomTypeDescriptor interface), the binding manager uses reflection to determine the public properties of the class and to allow binding to those properties.You cannot bind a DataGrid or the list portion of a ComboBox or a ListBox to an object that does not support the IList interface. If you bind a DataGrid to a list, and if the object that the list returns does not support ICustomTypeDescriptor, the DataGrid binds a column to each public property of the class. When you edit data in a bound control, the corresponding property of the bound object is updated immediately, unless the object implements the IEditableObject interface. If you change the property of the underlying object programmatically, the bound control is not notified of the changes unless the list object supports the IBindingList interface. QuickStart Samples
Databinding in Windows Forms If you installed the QuickStart samples on your computer, the
following subfolders contain additional samples:
http://samples.gotdotnet.com/quickstart/winforms/doc/winformsdata.aspx (http://samples.gotdotnet.com/quickstart/winforms/doc/winformsdata.aspx)
313334
(http://support.microsoft.com/kb/313334/EN-US/
)
HOW TO: Bind an Array of Structures to a Windows Form by Using Visual Basic .NET
317550
(http://support.microsoft.com/kb/317550/EN-US/
)
HOW TO: Use Visual C# .NET to Format a Windows Forms DataGrid That Is Bound to an Array
317383
(http://support.microsoft.com/kb/317383/EN-US/
)
HOW TO: Use Visual Basic .NET to Format a Windows Forms DataGrid That Is Bound to an Array
313636
(http://support.microsoft.com/kb/313636/EN-US/
)
HOW TO: Bind an ArrayList of Structures to a Windows Form by Using Visual C# .NET
313640
(http://support.microsoft.com/kb/313640/EN-US/
)
HOW TO: Bind an ArrayList or Collection of Objects to a Windows Form by Using Visual Basic .NET
313634
(http://support.microsoft.com/kb/313634/EN-US/
)
HOW TO: Bind an ArrayList of Objects to a Windows Form by Using Visual C# .NET
313335
(http://support.microsoft.com/kb/313335/EN-US/
)
HOW TO: Bind an Array of Structures to a Windows Form by Using Visual C# .NET
313639
(http://support.microsoft.com/kb/313639/EN-US/
)
HOW TO: Bind an Array of Objects to a Windows Form by Using Visual Basic .NET
313638
(http://support.microsoft.com/kb/313638/EN-US/
)
HOW TO: Bind an ArrayList or Collection of Structures to a Windows Form by Using Visual Basic .NET
313635
(http://support.microsoft.com/kb/313635/EN-US/
)
HOW TO: Bind an Array of Objects to a Windows Form by Using Visual C# .NET
316303
(http://support.microsoft.com/kb/316303/EN-US/
)
HOW TO: Bind a DataGrid Control to an ArrayList of Objects or Structures by Using Visual C# .NET
316302
(http://support.microsoft.com/kb/316302/EN-US/
)
HOW TO: Bind a DataGrid Control to an ArrayList of Objects or Structures by Using Visual Basic .NET
315786
(http://support.microsoft.com/kb/315786/EN-US/
)
HOW TO: Bind a DataGrid Control to an Array of Objects or Structures by Using Visual C# .NET
315784
(http://support.microsoft.com/kb/315784/EN-US/
)
HOW TO: Bind a DataGrid Control to an Array of Objects or Structures by Using Visual Basic .NET
Custom BindingYou can hook the Windows Forms framework to give your custom classes additional binding behaviors and to change the binding behavior of Windows Forms controls.Visual Studio .NET Help Documentation
Windows Forms Data Architecture Microsoft Knowledge Base Articleshttp://msdn2.microsoft.com/en-us/library/aa984214(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa984214(VS.71).aspx) Interfaces Related to Data Binding http://msdn2.microsoft.com/en-us/library/aa983621(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa983621(VS.71).aspx) 306227
(http://support.microsoft.com/kb/306227/EN-US/
)
HOW TO: Use a CheckBox Web Control in a DataGrid in Visual Studio .NET
318581
(http://support.microsoft.com/kb/318581/EN-US/
)
HOW TO: Extend the Windows Form DataGridTextBoxColumn Control to Custom-Format Data
319082
(http://support.microsoft.com/kb/319082/EN-US/
)
HOW TO: Extend the Windows Form DataGridTextBoxColumn to Display Data From Other Tables by Using Visual Basic .NET
325682
(http://support.microsoft.com/kb/325682/EN-US/
)
HOW TO: Implement a Custom DataView Class in Visual Basic .NET
TroubleshootingIf you experience problems, or if you have questions, you can refer to the MSDN newsgroups where you can share your experiences with your peers. You can also use the Microsoft Knowledge Base to search for articles about specific issues.
MSDN Newsgroups http://msdn.microsoft.com/newsgroups/ (http://msdn.microsoft.com/newsgroups/) Searching the Knowledge Base http://support.microsoft.com/search/?adv=1 (http://support.microsoft.com/search/?adv=1) APPLIES TO
| Article Translations
|

Back to the top
