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

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

问题描述

我制作了一个自定义的 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).

在表单的设计器中,自动添加了一个图像列.

In the designer of the form there is a ne image column added automatically.

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.

推荐答案

控件按预期运行.

原因

您在构造函数中添加了一个列,然后设计器序列化了该列.然后运行该应用程序并在构造函数中添加一列.所以你有 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.

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

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