I want to extend DataGridViewRow to have two custom properties .

I know I have to inherit from DataGridViewRow and add my custom properties .

I will be appreciate if somebody show me the road map .

有帮助吗?

解决方案

First inherit from DataGridViewRow (assume DataGridViewRowEx as class name), then after you have an instance of DataGridView assign the property RowTemplate to a new instance of DataGridViewRowEx:

dg.RowTemplate = new DataGridViewRowEx();

After that it should be ok, all rows added to the Rows collection will be of the same type as your inherited one (the Clone() method for DataGridViewRow create new rows of the same type as RowTemplate, see below).

public override object Clone()
{
    DataGridViewRow row;
    Type type = base.GetType();
    if (type == rowType)
    {
        row = new DataGridViewRow();
    }
    else
    {
        row = (DataGridViewRow) Activator.CreateInstance(type);
    }
    if (row != null)
    {
        base.CloneInternal(row);
        if (this.HasErrorText)
        {
            row.ErrorText = this.ErrorTextInternal;
        }
        if (base.HasHeaderCell)
        {
            row.HeaderCell = (DataGridViewRowHeaderCell) this.HeaderCell.Clone();
        }
        row.CloneCells(this);
    }
    return row;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top