Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 812919 - Last Review: December 11, 2006 - Revision: 1.9
BUG: The original data appears in the DataGrid control although you have removed the data from the DataSet object
On This Page
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 codeDataGrid1.DataSource = myDataSet.Tables("TableName") Microsoft Visual C# .NET codedataGrid1.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
- 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("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");
} - In Solution Explorer, right-click Form1,
and then click View Designer.
- 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");
} - On the Debug menu, click
Start to run the application.
- 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Â
(http://kbalertz.com/Feedback.aspx?kbNumber=313482/
)
INFO: Roadmap for Windows Forms data binding
APPLIES TO
- Microsoft ADO.NET 2.0
- Microsoft ADO.NET (included with the Windows .NET Framework 1.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
| kbvs2002sp1sweep kbcode kbpending kbdataview kbdatabinding kbcontrol kbsystemdata kbwindowsforms kbbug KB812919 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please