2012-09-27 42 views
0

我正在通過在MSDN網站Hosting custom controls within the datagridview的例子。我遇到的問題是,我在DataGridviewColumn上有一個Property,可以在設計時設置它,但它不會傳遞給列中的單個單元格。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

我完全忽略了在創建單元格時被調用的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; 
    } 
}