Custom DataGridView adds columns every time I build

与世无争的帅哥 提交于 2020-01-15 04:17:02

问题


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?


回答1:


Set AutoGenerateColumns to false.




回答2:


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



来源:https://stackoverflow.com/questions/21446678/custom-datagridview-adds-columns-every-time-i-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!