This documentation is archived and is not being maintained.

How to: Ensure the Selected Row in a Child Table Remains at the Correct Position

Oftentimes when you work with data binding in Windows Forms, you will display data in what is called a parent/child or master/details view. This refers to a data-binding scenario where data from the same source is displayed in two controls. Changing the selection in one control causes the data displayed in the second control to change. For example, the first control might contain a list of customers and the second a list of orders related to the selected customer in the first control.

Starting with the .NET Framework version 2.0, when you display data in a parent/child view you might have to take extra steps to make sure that the currently selected row in the child table is not reset to the first row of the table. In order to do this, you will have to cache the child table position and reset it after the parent table changes. Typically the child reset occurs the first time a field in a row of the parent table changes.

To Cache the Current Child Position

  1. Declare an integer variable to store the child list position and a Boolean variable to store whether to cache the child position.

  2. Handle the ListChanged event for the binding's CurrencyManager and check for a ListChangedType of Reset.

  3. Check the current position of the CurrencyManager. If it is greater than first entry in the list (typically 0), save it to a variable.

  4. Handle the parent list's CurrentChanged event for the parent currency manager. In the handler, set the Boolean value to indicate it is not a caching scenario. If the CurrentChanged occurs, the change to the parent is a list position change and not an item value change.

To Reset the Child Position

  1. Handle the PositionChanged event for the child binding's CurrencyManager.

  2. Reset the child table position to the cached position saved in the previous procedure.

Example

The following example demonstrates how to save the current position on the CurrencyManager.for a child table and reset the position after an edit is completed on the parent table. This example contains two DataGridView controls bound to two tables in a DataSet using a BindingSource component. A relation is established between the two tables and the relation is added to the DataSet. The position in the child table is initially set to the third row for demonstration purposes.

To test the code example, perform the following steps:

  1. Run the example.

  2. Make sure the Cache and reset position check box is selected.

  3. Click the Clear parent field button to cause a change in a field of the parent table. Notice that the selected row in the child table does not change.

  4. Close and run the example again. You need to do this because the reset behavior occurs only on the first change in the parent row.

  5. Clear the Cache and reset position check box.

  6. Click the Clear parent field button. Notice that the selected row in the child table changes to the first row.

Compiling the Code

This example requires:

  • References to the System, System.Data, System.Drawing, System.Windows.Forms, and System.XML assemblies.

For information about how to build this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-Line Building. You can also build this example in Visual Studio by pasting the code into a new project. How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio

See Also

Show: