2

I am writing an inherited DataGridView control which by default will have two columns. The DGV lives on a user control GeneralTabPanel which inherits from MainTabPanel which inherits from UserControl. Reason is MainTabPanel contains a virtual function that every inheriting panel must override.

The problem is whenever I look at the GeneralTabPanel in the Designer and I hit Build, Visual Studio Express 2013 will generate two columns in the Designer and add them to the DGV.

SuperDataGridView

using System.Windows.Forms;

namespace AnyGameEngineEditor {

    public class SuperDataGridView : DataGridView {

        public SuperDataGridView () {
            this.ColumnCount = 2;
        }
    }
}

GeneralTabPanel

namespace AnyGameEngineEditor {
    public partial class GeneralTabPanel : MainTabPanel {

        public GeneralTabPanel () {
            InitializeComponent ();
        }

    }
}

MainTabPanel

using System.Windows.Forms;

namespace AnyGameEngineEditor {
    public partial class MainTabPanel : UserControl {
        public MainTabPanel () {
            InitializeComponent ();
            this.Dock = DockStyle.Fill;
        }

    }
}

This is as barebones as I can get to reproducing the issue on my machine with VS2013 Express. This is how to reproduce (I have not actually tested this on another machine):

  1. Make a new Windows Forms project.
  2. Copy these three files into the root folder.
  3. Open up GeneralTabPanel in the designer.
  4. Open up the Designer .cs file for GeneralTabPanel.
  5. Build the project.
  6. There should be two new DataGridViewTextBoxCell variables at the bottom of the .cs file.
  7. Save the project and rebuild, two more variables should now be at the bottom.
  8. Repeat 5-7 until you're convinced something funny is going on.

What I found out is that if I comment out the Dock line in MainTabPanel, this doesn't occur.

So ultimately my question is why in the world is this happening?

Cœur
  • 37,241
  • 25
  • 195
  • 267
TreeTree
  • 3,200
  • 3
  • 27
  • 39
  • What happen if you comment the line `this.ColumnCount = 2;` or give some other number to the `columncount` – gmail user Jan 30 '14 at 02:30
  • @gmailuser That number determines how many columns are added each build. If I comment out that line, plus the lines below it due to null exceptions, then the grid will have zero columns. I need the grid to always have two columns. – TreeTree Jan 30 '14 at 02:35
  • I mean to say change the value just for debugging purpose. For eg. if you assign 3 columns, same thing happens or not. – gmail user Jan 30 '14 at 14:09
  • @gmailuser Yes it does, that's what I meant in my comment. – TreeTree Jan 30 '14 at 15:43

2 Answers2

0

Set AutoGenerateColumns to false.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
0

How about clear the datagridview each time opening the form? Form1.DataGridView1.Columns.Clear()

Joseph Wu
  • 4,786
  • 1
  • 21
  • 19