8.16. Using Windows Forms BindingNavigator and BindingSource Controls
Problem
You want an easy way to navigate and interact with records in a result set.
Solution
Use a BindingNavigator control that is bound to a BindingSource component. Bind the data controls to the BindingSource component by adding DataBinding objects.
Follow these steps:
Create a C# Windows Forms application,
BindWindowsFormBindingNavigator.Add the following controls to the
Form1design surface:BindingNavigatornameddepartmentBindingNavigatorBindingSourcenameddepartmentBindingSourceLabelwithTextproperty =DepartmentID:TextBoxnameddepartmentIDTextBoxLabelwithTextproperty =DepartmentNameTextBoxnameddepartmentNameTextBox
The completed layout of the Windows Form named
Form1is shown in Figure 8-31.
The C# code in Form1.cs in the project BindWindowsFormBindingNavigator is shown in Example 8-28. The solution fills a DataTable from the HumanResources.Department table in AdventureWorks and binds it to the data source of the BindingSource control. The BindingSource property of the BindingNavigator is set to the BindingSource control. The TextBox controls—departmentIDTextBox and departmentNameTextBox—are bound to the BindingSource control.

Figure 8-31. Layout for Form1 in BindWindowsFormBindingNavigator solution
Example 8-28. File: Form1.cs for BindWindowsFormBindingNavigator solution
using System; using System.Data; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access