3

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?

Community
  • 1
  • 1
Gunnarsi
  • 354
  • 6
  • 13
  • I have a similar issue, can anoyne help? https://stackoverflow.com/questions/45836304/c-sharp-adding-collection-of-custom-properties-from-the-property-grid-at-design – Asım Gündüz Aug 23 '17 at 13:15

1 Answers1

9

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

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 2
    Works like a charm. Finding this sooner would have saved me the better part of the day. – Daniel Jun 13 '14 at 22:48
  • Had a similar problem with a List property not being saved on custom DataGridViewColumn class in the VS designer. Can't believe this actually fixed it. Thank you!! – user3700562 Jul 29 '18 at 16:49