如何在DataRow上使用ExtendedProperties

5

一个C# DataTable有一个PropertyCollection扩展属性。该表中的DataColumn也有一个ExtendedProperties。为什么DataRow没有这个属性呢?

举例来说,如果我有多张表格并想要添加一些元数据以在视图中使用,我可以像这样做:

tbl.ExtendedProperties["class"] = "pandas";
tbl.Columns["name"].ExtendedProperties["class"] = "highlighted";

我该如何进一步深入并完成类似以下的操作:
tbl.Rows[0].ExtendedProperties["class"] = "highlighted";
1个回答

1
你可以尝试创建DataRow和DataTable的衍生版本。
    [Serializable]
public class CustomDataTable : DataTable
{
    public CustomDataTable()
        : base()
    {
    }

    public CustomDataTable(string tableName)
        : base(tableName)
    {
    }

    public CustomDataTable(string tableName, string tableNamespace)
        : base(tableName, tableNamespace)
    {
    }

    protected override Type GetRowType()
    {
        return typeof (CustomDataRow);
    }

    protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
    {
        return new CustomDataRow(builder);
    }
}

[Serializable]
public class CustomDataRow : DataRow
{
    public Dictionary<string, object> _extendedProperties = new Dictionary<string, object>();

    public Dictionary<string, object> ExtendedProperties {
        get { return _extendedProperties; }
    }

    public void SetAttribute(string name, object value)
    {
        ExtendedProperties.Add(name, value);
    }

    public CustomDataRow()
        : base(null)
    {
    }

    public CustomDataRow(DataRowBuilder builder)
        : base(builder)
    {
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接