* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
5.2 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| How to: Navigate Data | 03/30/2017 |
|
|
97360f7b-b181-4084-966a-4c62518f735b |
How to: Navigate Data in Windows Forms
In a Windows application, the easiest way to navigate through records in a data source is to bind a xref:System.Windows.Forms.BindingSource component to the data source and then bind controls to the xref:System.Windows.Forms.BindingSource. You can then use the built-in navigation method on the xref:System.Windows.Forms.BindingSource such a xref:System.Windows.Forms.BindingSource.MoveNext%2A, xref:System.Windows.Forms.BindingSource.MoveLast%2A, xref:System.Windows.Forms.BindingSource.MovePrevious%2A and xref:System.Windows.Forms.BindingSource.MoveFirst%2A. Using these methods will adjust the xref:System.Windows.Forms.BindingSource.Position%2A and xref:System.Windows.Forms.BindingSource.Current%2A properties of the xref:System.Windows.Forms.BindingSource appropriately. You can also find an item and set it as the current item by setting the xref:System.Windows.Forms.BindingSource.Position%2A property.
To increment the position in a data source
-
Set the xref:System.Windows.Forms.BindingSource.Position%2A property of the xref:System.Windows.Forms.BindingSource for your bound data to the record position to go to. The following example illustrates using the xref:System.Windows.Forms.BindingSource.MoveNext%2A method of the xref:System.Windows.Forms.BindingSource to increment the xref:System.Windows.Forms.BindingSource.Position%2A property when the
nextButtonis clicked. The xref:System.Windows.Forms.BindingSource is associated with theCustomerstable of a datasetNorthwind.Note
Setting the xref:System.Windows.Forms.BindingSource.Position%2A property to a value beyond the first or last record does not result in an error, as the .NET Framework will not allow you to set the position to a value outside the bounds of the list. If it is important in your application to know whether you have gone past the first or last record, include logic to test whether you will exceed the data element count.
[!code-csharpSystem.Windows.Forms.NavigatingData#4] [!code-vbSystem.Windows.Forms.NavigatingData#4]
To check whether you have passed the end or beginning
-
Create an event handler for the xref:System.Windows.Forms.BindingSource.PositionChanged event. In the handler, you can test whether the proposed position value has exceeded the actual data element count.
The following example illustrates how you can test whether you have reached the last data element. In the example, if you are at the last element, the Next button on the form is disabled.
Note
Be aware that, should you change the list you are navigating in code, you should re-enable the Next button, so that users may browse the entire length of the new list. Additionally, be aware that the above xref:System.Windows.Forms.BindingSource.PositionChanged event for the specific xref:System.Windows.Forms.BindingSource you are working with needs to be associated with its event-handling method. The following is an example of a method for handling the xref:System.Windows.Forms.BindingSource.PositionChanged event:
[!code-csharpSystem.Windows.Forms.NavigatingData#3] [!code-vbSystem.Windows.Forms.NavigatingData#3]
To find an item and set it as the current item
-
Find the record you wish to set as the current item. You can do this using the xref:System.Windows.Forms.BindingSource.Find%2A method of the xref:System.Windows.Forms.BindingSource, if your data source implements xref:System.ComponentModel.IBindingList. Some examples of data sources that implement xref:System.ComponentModel.IBindingList are xref:System.ComponentModel.BindingList%601 and xref:System.Data.DataView.
[!code-csharpSystem.Windows.Forms.NavigatingData#2] [!code-vbSystem.Windows.Forms.NavigatingData#2]