3

AC# DataTable 有一个 PropertyCollection ExtendedProperties。该表中的 DataColumn 也有一个ExtendedProperties 为什么 DataRow 没有这个?

因此,例如,如果我有多个表并且想要添加一些要在视图中使用的元数据,我可以执行以下操作:

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

我怎么能更进一步,做类似的事情

tbl.Rows[0].ExtendedProperties["class"] = "highlighted";
4

1 回答 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)
    {
    }
}
于 2017-02-16T14:47:06.407 回答