Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

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

I followed the article How to: Host Controls in Windows Forms DataGridView Cells and have inherited from DataGridViewTextBoxCell to create a custom cell for a DataGridView column.

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.

But I don't see how to make a cloneable CustomDataGridViewTextBoxCell. I can override the Clone method, but I can't call base.Clone() since it returns an object of the base class type, and I can't code a copy constructor because there is no copy constructor for DataGridViewTextBoxCell.

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

public override object Clone()
{
   return new MyDataGridViewEditingTextBoxCell(this);
}

with my overridden copy constructor:

public CustomDataGridViewTextBoxCell( CustomDataGridViewTextBoxCell cell)
  : base( cell )
{
   // Copy data of CustomDataGridViewTextBoxCell
   this.Flag = new Flag( cell.Flag );
   this.Data = cell.Data.Clone() as Data;
   // ...
}



Page view counter