在Designer中使用自定义DataGridView列时重复 [英] Custom DataGridView Column duplicates when using it in Designer

查看:400
本文介绍了在Designer中使用自定义DataGridView列时重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个自定义DataGridView组件,该组件内部具有标准的DataGridViewImageColumn。当我不需要在特定表中时,新属性会更改列的可见性。我将列添加到构造函数中,并在CellFormatting事件上对其进行更新。

I made a custom DataGridView component which has a standard DataGridViewImageColumn inside. A new property changes the visibility of the column when i don't need in in a particular table. I add the column in the constructor and update it on CellFormatting event. That is the part working like intended.

当我将控件放到新窗体上时,它将显示其中的新列。运行该程序将在网格中显示两个图像列。

When I drop the control onto a new form it shows up with the new column in it. Running the program results in two image columns in the grid.

新表单刚刚添加了组件并设置了Dock.Fill

A new form just added the component and set Dock.Fill

当我启动它而不更改任何内容时,它向我显示两列。第一个像应该那样工作,第二个总是显示丢失的x图像(其中没有数据,因此它们都显示了x)。

When i start it without changing anything it shows me two columns. The first is working like it should and the second one always shows the missing x image (Had no data in it so they show both the x).

private CustomDataGridView customDataGridView1;
private System.Windows.Forms.DataGridViewImageColumn dataGridViewImageColumn1;

当我继续以表格形式进行编辑时,有时VS会创建同一列的更多副本。要解决此问题,我必须不时清除DGV.Columns列表。

When i keep editing in the form it sometimes happens that VS creates even more copies of the same columns. To fix the problem i have to clear the DGV.Columns listing every now and then.

如何防止VS复制字段?

以下代码是重现该问题的最小部分。

The following code is the minimal part to reproduce the problem.

public class CustomDataGridView : DataGridView
{
    private DataGridViewImageColumn EditStatusIcons;

    private bool hasIcons = true;
    public bool HasIcons
    {
        get { return this.hasIcons; }
        set
        {
            if (this.Columns["EditIcons"] == null) return;

            this.Columns["EditIcons"].Visible = value;
            this.hasIcons = value;
        }
    }

    public CustomDataGridView()
    {
        this.EditStatusIcons = new System.Windows.Forms.DataGridViewImageColumn();

        this.EditStatusIcons.HeaderText = "";
        this.EditStatusIcons.Name = "EditStatusIcons";

        this.Columns.Add(this.EditStatusIcons);
    }
}

编辑:尝试使用 this.AutoGenerateColumns = false; 每次我自定义DataGridView添加列构建说。没什么改变。

I also tried to use this.AutoGenerateColumns = false; like in Custom DataGridView adds columns everytime I build said. Nothing changes.

清除表格上的列会删除工作列,并留下一列dataGridViewImageColumn1,它只是一个完整的空列,名称错误。

Clearing the columns listing on the form removes the working column and leaves a column dataGridViewImageColumn1 which is just a complete empty column with the wrong name.

推荐答案

控件的行为符合预期。

The control is acting as expected.

原因

您在构造函数中添加了一个列,然后设计器对柱。然后,您运行该应用程序,并在构造函数中添加一列。因此,您有2列。并以此方式继续,并且不限于2列。即使您不需要构建和运行,也足以打开包含网格的表单,并在表单中进行少量更改并保存。新列诞生了!

You added a column in constructor, then the designer serializes the column. Then you run the application and it adds a column in constructor. So you have 2 columns. And this continues this way and is not limited to 2 columns. Even you don't need to build and run, it's enough to open the form which contains the grid and make a small change in form and save it. A new column is born!

解决方案

根据您的要求,解决此问题,您可以考虑以下选项:

Based on your requirements, to solve the problem, you can consider these options:


  1. 检查控件是否处于设计模式,则不要添加列并仅在运行时添加它。

  1. Check if the control is in design mode, then don't add the column and add it only at run-time.

您可以检查控件是否包含现有的此类图像列,然后不添加其他图像列。您可以通过某些属性的类型来使列唯一。

You can check if the control contains an existing such image column then don't add another one. You can make the column unique by it's type of some properties.

您可以为控件创建自定义设计器,并将添加列的代码放入初始化中控制。这样,代码仅在将控件添加到窗体时运行。

You can create a custom designer for the control and put the code of adding the column in initialization of control. This way the code only runs firs time you add the control to the form.






有关解决方案的一些说明

解决方案之间的选择完全取决于您的要求,并且可以使用所有选项。但是请考虑以下有关解决方案的注意事项:

Choosing between solutions is completely based on your requirements and all options are available. But consider these notes about solutions:


  1. 关于第一种解决方案,请不要使用 DesignMode 属性,因为它在构造函数中不起作用。而是以这种方式执行检查:

  1. About the 1st solution, don't use DesignMode property because it doesn't work in constructor. Instead perform check this way:

if (System.ComponentModel.LicenseManager.UsageMode != LicenseUsageMode.Designtime)
{
    //The control is not in design mode, add the column here.
}


  • 关于第二种解决方案,例如,添加一个新的解决方案就足够了 MyCustomImageColumn 类型,仅检查是否存在类型为 MyCustomImageColumn 的列,如果存在,则不要添加其他列,因为其中之一就足够了。

  • About the 2nd solution, for example it's enough to add a new MyCustomImageColumn type and only check existence of a column of type MyCustomImageColumn and if it exists, don't add another one, because one of them is enough.

    这篇关于在Designer中使用自定义DataGridView列时重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆