I'm am working through an example over at the MSDN website for Hosting custom controls within the datagridview. The Problem that I am running into is that I have a Property on the DataGridviewColumn that can be set at design time but it is not getting passed down to individual cells in the column.
public class CalendarColumn : DataGridViewColumn
{
public string MyCoolNewProperty {get;set;}
public CalendarColumn() : base(new CalendarCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CalendarCell)))
{
throw new InvalidCastException("Must be a CalendarCell");
}
base.CellTemplate = value;
}
}
public override object Clone()
{
CalendarColumn obj = (CalendarColumn)base.Clone();
obj.MyCoolNewProperty = this.MyCoolNewProperty ;
return obj;
}
}
public class CalendarCell : DataGridViewTextBoxCell
{
public string MyCoolNewProperty {get;set;}
public CalendarCell()
: base()
{
// Use the short date format.
this.Style.Format = "d";
}
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
CalendarEditingControl ctl =
DataGridView.EditingControl as CalendarEditingControl;
ctl.Value = (DateTime)this.Value;
}
public override Type EditType
{
get
{
// Return the type of the editing contol that CalendarCell uses.
return typeof(CalendarEditingControl);
}
}
public override Type ValueType
{
get
{
// Return the type of the value that CalendarCell contains.
return typeof(DateTime);
}
}
public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return DateTime.Now;
}
}
public override object Clone()
{
CalendarCell obj = (CalendarCell)base.Clone();
obj.MyCoolNewProperty = this.MyCoolNewProperty ;
return obj;
}
}
How can i get the properties to propagate down to the cells and then the control?
I completely overlooked the InitializeEditingControl function within the CalendarCell class which gets called when the cell is created. From here I added the one line needed to access the properties of the column it belongs to.
public class CalendarCell : DataGridViewTextBoxCell
{
public string MyCoolNewProperty {get;set;}
public CalendarCell()
: base()
{
// Use the short date format.
this.Style.Format = "d";
}
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
CalendarEditingControl ctl =
DataGridView.EditingControl as CalendarEditingControl;
ctl.Value = (DateTime)this.Value;
// Access any Column Properties
ctl.MyCoolNewProperty = ((CalendarColumn)base.OwningColumn).MyCoolNewProperty;
}
public override Type EditType
{
get
{
// Return the type of the editing contol that CalendarCell uses.
return typeof(CalendarEditingControl);
}
}
public override Type ValueType
{
get
{
// Return the type of the value that CalendarCell contains.
return typeof(DateTime);
}
}
public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return DateTime.Now;
}
}
public override object Clone()
{
CalendarCell obj = (CalendarCell)base.Clone();
obj.MyCoolNewProperty = this.MyCoolNewProperty ;
return obj;
}
}