How to work around bug in DataSet?
The DataSet wont clear if ColumnNames Modified.
Any help or a work around would be greatly appreciated.
If you modify
dataSet.Tables[].Columns[].ColumnName
the dataSet will accumulate more and more columns. It appears that
something is holding on to a reference in the Tables.
To duplicate the problem use VS.NET to create a form with
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button loadButton;
Add the following private fields and a click event.
Notice that each time you click the button an addtional set of columns is
added.
// ^ ^ ^ Form Designer Code ^ ^ ^
DataSet dataSet = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT * FROM Jobs",
"User Id=sa;" +
"Password=;" +
"Initial Catalog=Northwind;"
);
private void loadButton_Click(object sender, System.EventArgs e)
{
// Supposed to remove old data and tables
dataSet.Clear();
sqlDataAdapter.Fill( dataSet );
// Decorate the column names
// Something about this causes the table structure to hang around.
DataTable dt = dataSet.Tables[0];
for (int i = 0; i < dt.Columns.Count; ++i)
dt.Columns[i].ColumnName += "!";
if ( dataGrid1.DataSource == null )
dataGrid1.SetDataBinding( dataSet, "Table" );
}
Thank...David
------------------------------------
--- Complete Listing ---------------
------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Data_SetGrid_Wont_Clear
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button loadButton;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.loadButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(280, 180);
this.dataGrid1.TabIndex = 0;
//
// loadButton
//
this.loadButton.Location = new System.Drawing.Point(8, 200);
this.loadButton.Name = "loadButton";
this.loadButton.TabIndex = 1;
this.loadButton.Text = "&Load";
this.loadButton.Click += new System.EventHandler(this.loadButton_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(296, 229);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.loadButton,
this.dataGrid1});
this.Name = "Form1";
this.Text = "DataSet/DataGrid Won\'t Clear";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
// ^ ^ ^ Form Designer Code ^ ^ ^
DataSet dataSet = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT * FROM Jobs",
"User Id=sa;" +
"Password=;" +
"Initial Catalog=Northwind;"
);
private void loadButton_Click(object sender, System.EventArgs e)
{
// Supposed to remove old data and tables
dataSet.Clear();
sqlDataAdapter.Fill( dataSet );
// Decorate the column names
// Something about this causes the table structure to hang around.
DataTable dt = dataSet.Tables[0];
for (int i = 0; i < dt.Columns.Count; ++i)
dt.Columns[i].ColumnName += "!";
if ( dataGrid1.DataSource == null )
dataGrid1.SetDataBinding( dataSet, "Table" );
}
}
}
You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.