BUG: The original data appears in the DataGrid control although you have removed the data from the DataSet object (812919)



The information in this article applies to:

  • Microsoft ADO.Net 2.0
  • Microsoft ADO.NET (included with the Windows .NET Framework 1.1)
  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

To make your data appear on a form, you bind a Microsoft Windows Forms DataGrid control to a DataTable object that is included in a DataSet object.

After your data has appeared in the DataGrid control, you use the Tables.Clear method to remove the DataTable object from the Tables collection of the bound DataSet object. However, although the bound DataSet object no longer contains any data, your original data continues to appear in the DataGrid control.

CAUSE

In a Microsoft Windows-based application, the CurrencyManager object manages a list of Binding objects. When you use a DataSet object together with a DataTable object for your data source, and you bind a DataGrid control to the DataSet object and the DataTable object, the CurrencyManager object manages this data source.

However, if you remove the DataTable object from the DataSet object, the related the CurrencyManager object that corresponds to the DataSet object is not updated. Therefore, the DataGrid control does not have this new information and the original data continues to appear in the DataGrid control.

WORKAROUND

To work around this problem, use the DataSource property instead of the SetDataBinding method to set the data source of the DataGrid control, as in the following sample code.

Microsoft Visual Basic .NET code
DataGrid1.DataSource = myDataSet.Tables("TableName")
Microsoft Visual C# .NET code
dataGrid1.DataSource = myDataSet.Tables["TableName"];

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section of this article.

MORE INFORMATION

Steps to reproduce the behavior

  1. In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, use Visual Basic .NET, Visual Basic 2005, Visual C# 2005, or Visual C# .NET to create a new Windows Application project. By default, Form1 is created.
  2. Add a DataGrid control to Form1.
    Note By default, the DataGrid control does not appear in Visual Studio 2005 toolbox. To obtain a DataGrid control, click Choose Toolbox Items on the Tools menu, click to select DataGrid on the .NET Framework Components tab, click Reset, and then click OK.
  3. Add a Button object to Form1. By default, the Button1 object is created.
  4. In design view, double-click Form1 to open the code for the Form1_Load event.
  5. Add the following code before the Form1_Load event code to create a DataSet object.

    Visual Basic .NET code

        ' Create a data source.
        Private myDataSet As DataSet = New DataSet
    Visual C# .NET code
          // Create a data source.
          private DataSet myDataSet = new DataSet();

  6. Replace the Form1_Load event code with the following code.

    Visual Basic .NET code

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Add a DataTable object to the DataSet object.
            myDataSet.Tables.Add("MyTable")
            myDataSet.Tables(0).Columns.Add("OriginalColumn")
            myDataSet.Tables(0).Rows.Add(New Object() {"Original Data"})
    
            ' Bind the DataGrid control to the data source.
            DataGrid1.SetDataBinding(myDataSet, "MyTable")
        End Sub
    Visual C# .NET code
       private void Form1_Load(object sender, System.EventArgs e)
       {
         // Add a DataTable object to the DataSet object.
         myDataSet.Tables.Add("MyTable");
         myDataSet.Tables[0].Columns.Add("OriginalColumn");
         myDataSet.Tables[0].Rows.Add(new Object[] {"Original Data"});
    
         // Bind the DataGrid control to the data source.
         dataGrid1.SetDataBinding(myDataSet, "MyTable");
       }

  7. In Solution Explorer, right-click Form1, and then click View Designer.
  8. In design view, double-click the Button1 object to add code for the Button1_Click event. Replace the code for the Button1_Click event with the following code.

    Visual Basic .NET code

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' Remove the DataTable object from the data source.
            myDataSet.Tables.Clear()
           
           ' Add a DataTable object to the DataSet object.
            myDataSet.Tables.Add("MyTable")
            myDataSet.Tables(0).Columns.Add("NewColumn")
            myDataSet.Tables(0).Rows.Add(New Object() {"New Data"})
    
            'DataGrid1.DataSource = myDataSet.Tables("MyTable")
            DataGrid1.SetDataBinding(myDataSet, "MyTable")
        End Sub
    Visual C# .NET code
       private void button1_Click(object sender, System.EventArgs e)
       {
          // Remove the DataTable object from the data source.
          myDataSet.Tables.Clear();
       
          // Add a DataTable object to the DataSet object.
          myDataSet.Tables.Add("MyTable");
          myDataSet.Tables[0].Columns.Add("NewColumn");
          myDataSet.Tables[0].Rows.Add(new Object[] {"New Data"});
      
          // Bind the DataGrid control to the data source.
          dataGrid1.SetDataBinding(myDataSet, "MyTable");		
       }

  9. On the Debug menu, click Start to run the application.
  10. Click Button1. Notice that the original value appears in the DataGrid control.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

313482 INFO: Roadmap for Windows Forms data binding


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2002sp1sweep kbcode kbpending kbDataview kbDataBinding kbControl kbSystemData kbWindowsForms kbbug KB812919 kbAudDeveloper