如何通过DataGridViewCellCollection更改类型返回以适合Custom DatagridViewCell

时间:2010-05-03 17:16:50

标签: c# datagridview properties accessibility

我的自定义DataGridViewCell有问题,实际上我试图写入我的datagridviewcell的自定义属性,但我不能,因为它不可访问。这是我的代码:

namespace MonthCalendarLibrary
{
    public class MonthCalendarCell : DataGridViewImageCell
    {

        public DateTime date { get; set; }

        public MonthCalendarCell() : base()
        {
            this.date = new DateTime();
            this.date = DateTime.Today;
        }


        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
                dataGridViewCellStyle);
            this.ReadOnly = false;

            MonthPanelEvent ctl = DataGridView.EditingControl as MonthPanelEvent;

        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that CalendarCell uses.
                return typeof(MonthPanelEvent);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that CalendarCell contains.
                return typeof(Image);
            }
        }


        public override object DefaultNewRowValue
        {
            get
            {
                // Use the current date and time as the default value.
                return Image.FromFile("C:\\blank.jpg");
            }
        }

        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            if (base.DataGridView != null)
            {
                Point point1 = base.DataGridView.CurrentCellAddress;
                if (((point1.X == e.ColumnIndex) && (point1.Y == e.RowIndex)) && (e.Button == MouseButtons.Left))
                {
                    if (base.DataGridView.EditMode != DataGridViewEditMode.EditProgrammatically)
                    {
                        base.DataGridView.BeginEdit(true);
                    }
                }
            }
        }

        public override object Clone()
        {
            MonthCalendarCell dataGridViewCell = base.Clone() as MonthCalendarCell;

            if (dataGridViewCell != null)
            {
                dataGridViewCell.date = this.date;
            }

            return dataGridViewCell;
        }

    }
}

当我尝试访问此属性时,这是我的代码:

this.Rows[i].Cells[j].date = this.jourDuMois.ElementAt(i * 7 + j);

我的问题是样本(我认为),我该如何访问这个属性?

我必须通过datagridviewcellcollection更改类型返回吗?或者有另一种解决方案。

提前谢谢。

致以最诚挚的问候,

P.S。 :对不起我的英语,我是法国人。

1 个答案:

答案 0 :(得分:1)

MonthCalendarCell cell = this.Rows[i].Cells[j] as MonthCalendarCell;
if(cell != null)
{
   cell.date = this.jourDuMois.ElementAt(i * 7 + j);
}