1

I have created a minimal example to show the issue I am having. I want to be able to see my DataGridView's columns at design time so I defined all of the columns in the VS Designer and set AutoGenerateColumns to false in the DGV's constructor and in the form's constructor.

The issue is that when I put the DGV into the form, it generates a column in the form's designer but the DGV already has the column in it's designer. So, the column is in both designers and both get added. I could put in something to remove the extra columns, but it seems like there should be a way that the form doesn't generate a column. How can I stop the form generating the column?

Note: there is no data binding in my example and you're seeing all of the relevant code.

public partial class CustomDGV : DataGridView
{
    public CustomDGV()
    {
        InitializeComponent();
        AutoGenerateColumns = false;
    }
}

// removed comments and dispose method to save space
partial class CustomDGV
{
    private void InitializeComponent()
    {
        this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
        this.SuspendLayout();
        this.Column1.HeaderText = "Column1";
        this.Column1.Name = "Column1";
        this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Column1});
        ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        customDGV1.AutoGenerateColumns = false;
    }
}

// removed comments and dispose method to save space
partial class Form1
{
    private System.ComponentModel.IContainer components = null;
    private void InitializeComponent()
    {
        this.customDGV1 = new TestWinFormsApp.CustomDGV();
        this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.customDGV1)).BeginInit();
        this.SuspendLayout();
        this.customDGV1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.customDGV1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.dataGridViewTextBoxColumn1});
        this.customDGV1.Location = new System.Drawing.Point(28, 21);
        this.customDGV1.Name = "customDGV1";
        this.customDGV1.Size = new System.Drawing.Size(364, 150);
        this.customDGV1.TabIndex = 0;
        this.dataGridViewTextBoxColumn1.HeaderText = "Column1";
        this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
        this.ClientSize = new System.Drawing.Size(602, 189);
        this.Controls.Add(this.customDGV1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.customDGV1)).EndInit();
        this.ResumeLayout(false);

    }
    private CustomDGV customDGV1;

    // I don't believe this column should be here
    private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
}

How I add the column... How I added the column

Design Time... Design Time

Run Time... Run Time

Blake Thingstad
  • 1,639
  • 2
  • 12
  • 19

0 Answers0