문제

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