2013-06-26 19 views
0

我想擴展DataGridViewRow以擁有兩個自定義屬性。在C中擴展DataGridViewRow#

我知道我必須從一個DataGridViewRow繼承並添加我的自定義屬性。

如果有人給我看路線圖,我將不勝感激。

回答

3
從一個DataGridViewRow(假設DataGridViewRowEx爲類名)

首先繼承,那麼以後你的DataGridView的一個實例的屬性RowTemplate分配給DataGridViewRowEx的新實例:

dg.RowTemplate = new DataGridViewRowEx(); 

之後,它應該是好的,所有添加到Rows集合中的行將與繼承的行類型相同(DataGridViewRow的Clone()方法創建與RowTemplate相同類型的新行,請參見下文)。

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; 
}