Microsoft KB Archive/812919

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

Article ID: 812919

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft ADO.NET 2.0
 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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(&quot;TableName&quot;) Microsoft Visual C# .NET code dataGrid1.DataSource = myDataSet.Tables[&quot;TableName&quot;];



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section of this article.



Steps to reproduce the behavior
 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. 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. Add a Button object to Form1. By default, the Button1 object is created. In design view, double-click Form1 to open the code for the Form1_Load event. 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;

 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(&quot;MyTable&quot;) myDataSet.Tables(0).Columns.Add(&quot;OriginalColumn&quot;) myDataSet.Tables(0).Rows.Add(New Object {&quot;Original Data&quot;})

' Bind the DataGrid control to the data source. DataGrid1.SetDataBinding(myDataSet, &quot;MyTable&quot;) 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(&quot;MyTable&quot;); myDataSet.Tables[0].Columns.Add(&quot;OriginalColumn&quot;); myDataSet.Tables[0].Rows.Add(new Object[] {&quot;Original Data&quot;});

// Bind the DataGrid control to the data source. dataGrid1.SetDataBinding(myDataSet, &quot;MyTable&quot;); }

</li> In Solution Explorer, right-click Form1, and then click View Designer.</li> 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(&quot;MyTable&quot;) myDataSet.Tables(0).Columns.Add(&quot;NewColumn&quot;) myDataSet.Tables(0).Rows.Add(New Object {&quot;New Data&quot;})

'DataGrid1.DataSource = myDataSet.Tables(&quot;MyTable&quot;) DataGrid1.SetDataBinding(myDataSet, &quot;MyTable&quot;) 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(&quot;MyTable&quot;); myDataSet.Tables[0].Columns.Add(&quot;NewColumn&quot;); myDataSet.Tables[0].Rows.Add(new Object[] {&quot;New Data&quot;}); // Bind the DataGrid control to the data source. dataGrid1.SetDataBinding(myDataSet, &quot;MyTable&quot;); }

</li> On the Debug menu, click Start to run the application.</li> Click Button1. Notice that the original value appears in the DataGrid control.</li></ol>

<div class="references_section">