How do I override the DataGridViewTextBox.Clone method ? In general, how do I implement ICloneable in a derived class ?

Platform: WPF| Category: Annotations

When the DataGridView needs to create a new cell in the column the method DataGridViewTextBoxCell.Clone is called in order to duplicate the ’DataGridViewColumn.CellTemplate’. It’s perfectly logical.

You can override the Clone method, but you can’t call base.Clone() since it returns an object of the base class type and you can’t code a copy constructor because there is no copy constructor for DataGridViewTextBoxCell.

With a base copy constructor you could code a Clone method like this :

[C#]

public override object Clone()
{
   return new MyDataGridViewEditingTextBoxCell(this);
}
With your overridden copy constructor you could code a Clone method like this :
public CustomDataGridViewTextBoxCell( CustomDataGridViewTextBoxCell cell)
  : base( cell )
{
   // Copy data of CustomDataGridViewTextBoxCell
   this.Flag = new Flag( cell.Flag );
   this.Data = cell.Data.Clone() as Data;
   // ...
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.