Custom property won't save when changed in pro

2020-04-11 08:34发布

I've created a custom column for DataGridView, and the reason is that I want to add a property (type) to a column. I right click the DataGridView and select "Edit columns...". Then when I select the column that is my custom column type I'm able to edit the property, but if I clike "OK" after editing and then go to "Edit columns..." again the value that I assigned to my property is gone.

Here is my code:

public class CustomColumn : DataGridViewColumn
{
    [DisplayName("Type")]
    [Category("Custom Property")]
    public String type { get; set; }

    public CustomColumn()
        : base(new DataGridViewTextBoxCell())
    {
    }
}

And an image of the property window:

Image of the propert windows

Can someone tell me what I'm doing wrong, or what I need to add so that when I change the value in the property window, that value is assigned to the property?

1条回答
Fickle 薄情
2楼-- · 2020-04-11 09:21

I think you need to override the Clone() method in order for that to work:

public class CustomColumn : DataGridViewColumn {

  public CustomColumn()
    : base(new DataGridViewTextBoxCell()) {
  }

  [DisplayName("Type")]
  [Category("Custom Property")]
  public String type { get; set; }

  public override object Clone() {
    CustomColumn copy = base.Clone() as CustomColumn;
    copy.type = type;
    return copy;
  }
}

See Custom properties on overridden DataViewColumn do not save

查看更多
登录 后发表回答