Note that there are some explanatory texts on larger screens.

plurals
  1. PODataGridView repeatedly recreating columns
    text
    copied!<p>I have a problem when assigning the <code>DataSource</code> propery on a <code>DataGridView</code> control. My <code>DataSource</code> is a <code>DataTable</code>'s <code>DefaultView</code> and, as expected, columns are automatically created in the <code>DataGridView</code> to match those in the <code>DataTable</code> when I assign it.</p> <p>What happens next is that the columns seem to be automatically removed and recreated a further 2 times by the <code>DataGridView</code>. Why would this happen?</p> <p>In a form's constructor:</p> <pre><code>//A DataTable is created with 5 columns //The DataTable is populated with some rows. myDgv.AutoGenerateColumns = true; myDgv.DataSource = myDataTable.DefaultView; // myDgv.ColumnAdded event is fired 5 times. // WHY: myDgv.ColumnRemoved event is fired 5 times. // WHY: myDgv.ColumnAdded event is fired 5 times. // WHY: myDgv.ColumnRemoved event is fired 5 times. // WHY: myDgv.ColumnAdded event is fired 5 times. </code></pre> <hr> <p><strong>Edit: Added a (hopefully) self contained example. If I set breakpoints in the event handlers, I hit the 'Added' one 6 times and the 'Removed' one 4 times. The DataTable contains 2 columns and I never ask for any columns to be removed in my code.</strong></p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace asdasdgf { public class Form1 : Form { public Form1() { InitializeComponent(); DataTable dt = new DataTable(); dt.Columns.Add("Col1", typeof(int)); dt.Columns.Add("Col2", typeof(string)); foreach (int i in Enumerable.Range(0, 10)) { var row = dt.NewRow(); row["Col1"] = i; row["Col2"] = "stackoverflow"; dt.Rows.Add(row); } dataGridView1.ColumnAdded += new DataGridViewColumnEventHandler(dataGridView1_ColumnAdded); dataGridView1.ColumnRemoved += new DataGridViewColumnEventHandler(dataGridView1_ColumnRemoved); dataGridView1.DataSource = dt.DefaultView; } void dataGridView1_ColumnRemoved(object sender, DataGridViewColumnEventArgs e) { // Break here } void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e) { // Break here } // Form1.Designer.cs contents: #region Windows Form Designer generated code private System.ComponentModel.IContainer components = null; private System.Windows.Forms.DataGridView dataGridView1; protected override void Dispose(bool disposing) { if (disposing &amp;&amp; (components != null)) { components.Dispose(); } base.Dispose(disposing); } /// &lt;summary&gt; /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// &lt;/summary&gt; private void InitializeComponent() { this.dataGridView1 = new System.Windows.Forms.DataGridView(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Location = new System.Drawing.Point(12, 41); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Size = new System.Drawing.Size(240, 150); this.dataGridView1.TabIndex = 0; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 264); this.Controls.Add(this.dataGridView1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } #endregion } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload