扩展DataGridViewColumn和DataGridViewCell,DataGridView现有的列列类型有六种:DataGridViewTextBoxColumn ,DataGridViewButtonColumn ,DataGridViewComboBoxColumn , DataGridViewImageColumn ,DataGridViewLinkColumn ,DataGridViewCheckBoxColumn 。 你可以扩展你自己需要的如:DataGridViewTreeColumn,DataGridViewTreeCell,给个示范:public class CalendarCell : DataGridViewTextBoxCell { 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; } } }
DataGridViewImageColumn ,DataGridViewLinkColumn ,DataGridViewCheckBoxColumn 。
你可以扩展你自己需要的如:DataGridViewTreeColumn,DataGridViewTreeCell,给个示范:public class CalendarCell : DataGridViewTextBoxCell
{ 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;
}
}
}