本文介绍了每次构建时,自定义DataGridView都会添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个继承的 DataGridView 控件,默认情况下它将具有两列.DGV驻留在用户控件 GeneralTabPanel 上,该控件继承自 MainTabPanel ,其继承自 UserControl .原因是 MainTabPanel 包含一个虚函数,每个继承面板都必须覆盖该虚函数.

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.

问题是,每当我在Designer中查看 GeneralTabPanel 并单击Build时, Visual Studio Express 2013 都会在Designer中生成两列并将其添加到DGV.

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;
        }

    }
}

这是准系统,我可以使用VS2013 Express在我的计算机上重现该问题.复制方法(我尚未在另一台机器上实际测试过):

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. 创建一个新的Windows Forms项目.
  2. 将这三个文件复制到根文件夹中.
  3. 在设计器中打开 GeneralTabPanel .
  4. 打开 GeneralTabPanel 的Designer .cs文件.
  5. 构建项目.
  6. .cs文件底部应有两个新的 DataGridViewTextBoxCell 变量.
  7. 保存项目并重建,现在应该在底部再添加两个变量.
  8. 重复5-7,直到您确信发生了什么有趣的事情.
  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.

我发现,如果我注释掉 MainTabPanel 中的 Dock 行,则不会发生这种情况.

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?

推荐答案

每次打开表单时清除datagridview如何?Form1.DataGridView1.Columns.Clear()

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

这篇关于每次构建时,自定义DataGridView都会添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 15:35