3

I have this problem, I created my own datagridviewcolumn and I wish add some properties that you can change in designtime editing... here is my code:

private int nMaxLength;
[Description("Fondoscala valore"), Category("Sea")]
public int MaxLength
{
    get { return nMaxLength; }
    set { nMaxLength = value; }
}

and in fact is ok, when you open the column editor, you see this property under the Sea category and you can change, but when you changed it, if you go to the .Designer.cs file, you see the MaxLength value to 0.. no change... what's the problem?? thanks in advance

ghiboz
  • 7,863
  • 21
  • 85
  • 131

1 Answers1

8

The Forms Designer does some internal trickery in order to allow you to change the column type (e.g. from DataGridViewTextBoxColumn to DataGridViewButtonColumn) at design time. As a result of this, the designer relies on your subclass of DataGridViewColumn having a correctly-implemented Clone() method, i.e:

public override object Clone() {
    MyDataGridViewColumn that = (MyDataGridViewColumn)base.Clone();
    that.MaxLength = this.MaxLength;
    return that;
}

If you do not override the Clone() method, the designer will not commit any changes you make to custom property values.

Bradley Smith
  • 13,353
  • 4
  • 44
  • 57
  • Here's a 3-year-late +1. Ran into exactly the same problem. This was a succinct and correct solution to it. – KChaloux Mar 13 '13 at 19:45
  • Thanks for that Bradley, for some reason however, I'm unable to access the value at run time - it's always empty, is there a piece of the puzzle I'm still missing? – mattpm Jul 17 '13 at 06:07
  • I should clarify, I can access the value from the control on the form, but I need to be able to access the value internally within the control to drive other functionality. – mattpm Jul 17 '13 at 06:59
  • @mattpm I'd recommend opening a new question on this topic and including some of your code. The cause of your problem isn't immediately apparent. – Bradley Smith Jul 17 '13 at 07:09
  • Thanks Bradley, I have done and posted the complete code (which is just a sample from MS which I'm wanting to add some design-time parameters to). http://stackoverflow.com/questions/17711964/datagridview-custom-column-accessing-design-time-parameters-at-run-time – mattpm Jul 17 '13 at 23:25