0

我正在通过 MSDN 网站上的一个示例在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;
    }
}

我怎样才能让属性传播到单元格,然后传播到控件?

4

1 回答 1

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;
    }
}
于 2012-09-28T15:27:35.900 回答