c# - 如何在 DataRow 上使用 ExtendedProperties

标签 c# datatable datarow extended-properties

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

因此,例如,如果我有多个表并想添加一些要在 View 中使用的元数据,我可以这样做:

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

我怎样才能更进一步并做类似的事情

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

最佳答案

您可以尝试创建 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)
    {
    }
}

关于c# - 如何在 DataRow 上使用 ExtendedProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39208528/

相关文章:

c# - WPF ItemsControl 不刷新

c# - 如何格式化 DataRepeater 中的数字或字符串?

jquery - 数据表精确单词搜索

c# - 在 C# 中使用 Javascript 序列化器将 DataTable 转换为嵌套 JSON

java - 需要帮助将 c# 加密转换为 java (android) 加密以实现交叉兼容性

c# - "The LINQ expression node type ' 调用 ' is not supported in LINQ to Entities"- 难倒了!

c# - 从 DataRow 获取值(value)的区别

c# - 将单行从一个数据表复制到另一个

C#/winforms : how to best bind a propertygrid and a System. 数据.数据行

c# - 让 EntityFramework 尊重 mysql max_user_connections