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



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)

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:
  1. 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("TableName").RowChanged, New DataRowChangeEventHandler(AddressOf Row_Changed)

    Visual C# .NET Code

       // Event handler for RowChange event
       objds.Tables["authors"].RowChanged += new System.Data.DataRowChangeEventHandler(Row_Changed);
  2. 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, "authors"), 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, "authors"];
             // 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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Microsoft Visual Studio .NET, open a new Windows application using either Visual Basic .NET or Visual C# .NET.
  2. On the View menu, click Solution Explorer.
  3. In Solution Explorer, right-click WindowsApplication1, point to Add, and then click Add New Item.
  4. Under Templates, select Data Form Wizard.
  5. In Data Form Wizard, click Next, and then type ds.
  6. Click Next, and then click New Connection.
  7. On the Connection tab, type the name of your local SQL Server, select the pubs database, and then click Next.
  8. Select both the authors table and the titleauthor table, and then click > (the right arrow button).
  9. Click Next.
  10. Type title_author in the Name textbox.
  11. Select authors under Parent Table, and then select titleauthor under Child Table.
  12. Select au_id under Keys, and then click >.
  13. Click Next two times, and then click Finish to close the wizard.
  14. 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());
      }
  15. In Solution Explorer, right-click WindowsApplication1, and then click Properties.
  16. Under Startup object, select DataForm1.
  17. On the Debug menu, click Start.
  18. Click Load, and then type a new record in the parent DataGrid.
  19. Click the child DataGrid.

    Notice that the value in au_id is not populated to the child DataGrid.

REFERENCES

For more information, visit the following MSDN Web sites:

Modification Type:MinorLast Reviewed:3/9/2006
Keywords:kbvs2002sp1sweep kbdisplay kbCtrl kbControl kbWindowsForms kbtable kbDesigner kbDataBinding kbDatabase kbbug KB816227 kbAudDeveloper