我正在MSDN网站上为Hosting custom controls within the datagridview做一个例子。我遇到的问题是我在DataGridviewColumn上有一个属性,可以在设计时设置,但它不会传递给列中的单个单元格。
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;
}
}
如何让属性传播到单元格然后传播到控件?
答案 0 :(得分:0)
我完全忽略了CalendarCell类中的InitializeEditingControl函数,该函数在创建单元格时被调用。从这里我添加了访问它所属列的属性所需的一行。
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;
}
}