I have a problem when assigning the DataSource
propery on a DataGridView
control. My DataSource
is a DataTable
's DefaultView
and, as expected, columns are automatically created in the DataGridView
to match those in the DataTable
when I assign it.
What happens next is that the columns seem to be automatically removed and recreated a further 2 times by the DataGridView
. Why would this happen?
In a form's constructor:
//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.
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.
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 && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
}
}