Custom property won't save when changed in property window

亡梦爱人 提交于 2019-12-02 01:15:14

问题


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:

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:


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



来源:https://stackoverflow.com/questions/10816916/custom-property-wont-save-when-changed-in-property-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!