gpt4 book ai didi

c# - 如何在 DataRow 上使用 ExtendedProperties

转载 作者:行者123 更新时间:2023-11-30 17:36:34 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com