0

DataGridViewRow を拡張して、2 つのカスタム プロパティを持たせたいと考えています。

DataGridViewRow から継承し、カスタム プロパティを追加する必要があることはわかっています。

誰か ロードマップ を 見せ て くれる と ありがたい です .

4

1 に答える 1

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;
}
于 2013-06-26T10:25:56.913 に答える