在VS Designer中使用自定义DataGridView时在运行时重复列

时间:2017-12-08 15:59:24

标签: c# winforms datagridview

我创建了一个最小的例子来展示我遇到的问题。我希望能够在设计时看到我的DataGridView列,因此我在VS Designer中定义了所有列,并在DGV的构造函数和表单构造函数中将AutoGenerateColumns设置为false

问题在于,当我将DGV放入表单时,它会在表单设计器中生成一列,但DGV已经在其设计器中有列。因此,该列在两位设计师中都得到了补充。我可以放入一些东西来删除额外的列,但似乎应该有一种表单不会生成列的方式。如何停止生成列的表单?

注意:我的示例中没有数据绑定,您可以看到所有相关代码。

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 added the column

设计时间...... Design Time

运行时间...... Run Time

0 个答案:

没有答案