I'm facing this strange problem while working with DataGridView. In my application that i'm building, there is a form with some tabs, and im using the dataGridView Controls in some of the tabs.

These dataGridView controls have already been configured, with a number of columns already added, and has been working perfectly fine for a long time.

Today when i open the project, i see that one of the dataGridView has all the columns mysteriously deleted! I had to re create the entire control all over again. As soon as i finish that, i see that its happened to another datagridview in the same form!!
I cannot add the same columns again as it says that the name is already in use by another component. So basically i will have to delete the dataGridView and recreate it again if i want the same column names!

Has anyone else come across this weird problem? I've only found one reference to something like this on google here:
http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/46d8c2b8-3cd7-449a-8cbf-424b67002111

but the solution wasent really helpful. any hints/help would be appreciated!

Recommended Answers

All 9 Replies

Out of curiosity, was this grid dragged from a Data Source?

Also, though maybe not your preference, I believe you could prevent this from reoccurring if you transfer the form.designer.cs code to your form.cs. You can do this in increments as you are modifying the grid. This way, the designer will not muck it up again with whatever that bug is about. Just a thought.

No, the data is not really binded to a data source.

The columns were manually created, and the data is fetched from a database, its formatted accordingly and inserted in corresponding locations.

You mean i can simply copy and paste the code from the designer.cs to the .cs file? Should that be done inside the Form_load or before InitializeComponent()?

You mean i can simply copy and paste the code from the designer.cs to the .cs file? Should that be done inside the Form_load or before InitializeComponent()?

Do it inside the constructor, which is where the InitializeComponent() gets called. Do it after the call to IntializeComponent(), as that is where the control is created. If you wanted to, you could move all of the designer code into your form.cs file. Only problem with doing this is that you will get a designer error screen with prompt to view stack should the designer get confused and you try to view the form in design mode. However, moving the column information from the form.designer.cs file to the form.cs file following the InitializeComponent() call in the form's constructor I think would synchronize. Let me give a demo ride on my machine and get back to you...

Crapiola. I forgot the designer will no longer allow you to use its design editor once you move out that code. Your project will compile and run OK with the code moved into your form.cs file from the designer.cs file, but the designer will give that Show Call Stack() designer error page when you try to view the form in design mode.

Anyway, to prevent the designer from obliterating your code, you could copy the contents of the IntializeComponent() method into your form.cs constructor, then move it back to the form.designer.cs method whenever you want to use the designer to edit the form. I know it's not much help, but at least you can be sure the designer cannot muck up the code as long as it is in the form.cs file.

EDIT: Just to be clear, the designer looks for the InitializeComponent() method to determine the form's design. Obviously, if you were to cut/paste that method into your form.cs file, the designer would still edit that method, but now it would be editing your form.cs file, which doesn't give any advantage. So if you decide to implement a workaround for this bug, you need to put that contents of the InitializeComponent() method somewhere the designer can't touch it, but again, to use the designer to view the form, you would need to put this code back into that InitializeComponent() method wherever it is (form.cs or form.designer.cs file). Does that make sense?

For example... Here I have added a method to my form.cs file called InitializeComponent2(), which I am calling from the IntializeComponent() method in the form.designer.cs file:

partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            InitializeComponent2();
        }

        private void MyInitializeComponent()
        {
        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column5;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column6;

    }

I have moved the original code from the IntializeComponent() method into this IntializeComponent2() method in my form.cs file:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent2()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2,
            this.Column3,
            this.Column4,
            this.Column5,
            this.Column6});
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(758, 266);
            this.dataGridView1.TabIndex = 0;
            // 
            // Column1
            // 
            this.Column1.HeaderText = "Column1";
            this.Column1.Name = "Column1";
            // 
            // Column2
            // 
            this.Column2.HeaderText = "Column2";
            this.Column2.Name = "Column2";
            // 
            // Column3
            // 
            this.Column3.HeaderText = "Column3";
            this.Column3.Name = "Column3";
            // 
            // Column4
            // 
            this.Column4.HeaderText = "Column4";
            this.Column4.Name = "Column4";
            // 
            // Column5
            // 
            this.Column5.HeaderText = "Column5";
            this.Column5.Name = "Column5";
            // 
            // Column6
            // 
            this.Column6.HeaderText = "Column6";
            this.Column6.Name = "Column6";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(758, 266);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }

Now, this code will compile and run just fine. However, if you attempt to view the form in designer mode, you will see a screen saying there are errors and and prompt to show call stack() and fix them, etc. You can just close this window and compile and run.

Bottom line, until this code is put back into the original InitializeComponent() method, the designer is confused and doesn't know how to put you into the proper design view mode. In this example, to alleviate this error and resume form design/view mode, just copy the code from the IntializeComponent2() (in form.cs file) method back into the IntializeComponent() method (in form.designer.cs file) so the designer can interpret the form view once again...

hey thanks for that. It does work.. had to make some minor modifications but it works.

But it still is annoying that i have to do this to protect the datagridview! It very strange how one fine day the dataGridView decides to clear all its columns! This surely seems to be an annoying bug that Microsoft has not taken care of or even mentioned it anywhere! At least i could'nt find it.

Anyway, thanks for ur help, and i can just pray that the other controls dont have mood swings like dataGridView!!

LOL--though losing work is no laughing matter indeed!

Check this UI gridview control tutorial to see if it can fix your problem.

int i = 0;
foreach (GridColumn column in ketticGrid.Columns)
{
    if (column is GridDataColumn)
    {
        GridDataColumn col = column as GridDataColumn;
        if (col != null)
        {
            col.Width = 120;
            col.HeaderText = "Column count : " + i.ToString();
            i++;
        }
    }
}

I had a similar issue, which I solved by renomming all my datagridview's columns to new, unique names (starting with _ and all lowercase, but i doubt this fact has any effect).
Previous names matched some local variables in some events (or private methods called from such events), except the variable name case was different.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.