Microsoft KB Archive/816227

= BUG: A new record that is added to a parent DataGrid control does not populate the foreign key column in the child DataGrid control =

Article ID: 816227

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft ADO.NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



SYMPTOMS
In your Windows application Windows Form, you have added two DataGrid controls that have the Parent-Child relationship between them. After you add a new record in the parent DataGrid, and then you click the child DataGrid to populate the foreign key column, the column value is not displayed in the child DataGrid. The new record is displayed in the child DataGrid only after you click a different row in the parent DataGrid, and then you click the child DataGrid.



CAUSE
The problem occurs because the newly added record of the parent DataGrid is not added to the Dataset.

The relationship between the parent table and the child table is defined in the Dataset. Therefore, the foreign key column is not populated to the child DataGrid. When you click any other parent DataGrid record, and then click the child DataGrid, the new record is added to the DataSet, and the record is displayed in the child DataGrid.



RESOLUTION
To resolve this problem, call the Refresh method of the Currency Manager for the DataSet before you click the child DataGrid. You can call the Refresh method in the RowChange event of the parent table.

To do this, follow these steps:   Add the following code to add the event handler for the RowChange event in the Form_Load function.

Visual Basic .NET Code
' Event handler for RowChange event AddHandler DataSetName.Tables(&quot;TableName&quot;).RowChanged, New DataRowChangeEventHandler(AddressOf Row_Changed)

Visual C# .NET Code
// Event handler for RowChange event objds.Tables[&quot;authors&quot;].RowChanged += new System.Data.DataRowChangeEventHandler(Row_Changed);   Add the following code for the RowChange event to refresh the Currency Manager:

Visual Basic .NET Code
Private Sub Row_Changed(ByVal sender As Object, ByVal e As DataRowChangeEventArgs) ' Get the Currency Manager Dim myCurrencyManager As CurrencyManager = CType(Me.BindingContext(objds, &quot;authors&quot;), CurrencyManager) ' Refresh the Currency Manager myCurrencyManager.Refresh End Sub

Visual C# .NET Code
private void Row_Changed( object sender, System.Data.DataRowChangeEventArgs e ) {        // Get the Currency Manager CurrencyManager myCurrencyManager = (CurrencyManager)this.BindingContext[objds, &quot;authors&quot;]; // Refresh the Currency Manager myCurrencyManager.Refresh; } 



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
 In Microsoft Visual Studio .NET, open a new Windows application using either Visual Basic .NET or Visual C# .NET. On the View menu, click Solution Explorer. In Solution Explorer, right-click WindowsApplication1, point to Add, and then click Add New Item.</li> Under Templates, select Data Form Wizard.</li> In Data Form Wizard, click Next, and then type ds .</li> Click Next, and then click New Connection.</li> On the Connection tab, type the name of your local SQL Server, select the pubs database, and then click Next.</li> Select both the authors table and the titleauthor table, and then click > (the right arrow button).</li> Click Next.</li> Type title_author in the Name textbox.</li> Select authors under Parent Table, and then select titleauthor under Child Table.</li> Select au_id under Keys, and then click >.</li> Click Next two times, and then click Finish to close the wizard.</li>  Perform this step only in Visual C# .NET:

On the View menu, click Code, and then add the following code at the end of the existing code: [STAThread] static void Main {    Application.Run(new DataForm1); } </li> In Solution Explorer, right-click WindowsApplication1, and then click Properties.</li> Under Startup object, select DataForm1.</li> On the Debug menu, click Start.</li> Click Load, and then type a new record in the parent DataGrid.</li> <li>Click the child DataGrid.

Notice that the value in au_id is not populated to the child DataGrid.</li></ol>

<div class="references_section">