Custom DataGridView column duplicates it when used in Designer

I created a custom component DataGridView, inside which there is a standard DataGridViewImageColumn. The new property changes the visibility of the column when I do not need a separate table. I add a column to the constructor and update it on the CellFormatting event. This is the part that works as intended.

When I drop the control into a new form, it appears with a new column in it. Running the program results in two columns of images in the grid.

Screenshot in designer

The new form just added a component and installed Dock.Fill

Screenshot from running application

When I start it without changing anything, it shows me two columns. The first works as it should, and the second always shows the missing x-image (without data in it, so that they display as x).

ne, .

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

, , VS . , DGV.Columns.

VS ?

.

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 , . .

dataGridViewImageColumn1, .

+3
2

, .

, . . , 2 . . , , , . !

, , :

  • , , .

  • , , . .

  • . , .


. :

  • 1- DesignMode, . :

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

  • . , , . DataGridView DataGridViewDesigner, , , - , . , ToolBoxItem , . , .
+3

, , , , "". MSDN, . , Reza, .

, CoolDadTx, , DataGridView UserControl, . :

Inheritance , .

​​, usercontrol , gridviewproperty .

- , 17 2014 .

, UserControl , . . , DesignTime if-statement, :

public class CustomControl : UserControl
{
    private DataGridViewImageColumn EditStatusIcons;
    public DataGridView DGV;

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

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

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

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

        this.DGV= new DataGridView();
        this.DGV.Dock = DockStyle.Fill;
        this.DGV.Columns.Add(this.EditStatusIcons);

        this.Controls.Add(this.DGV);
    }
}
+1

All Articles